Skip to content

Commit

Permalink
added attr fling_handle_id, cleaned code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
imbryk committed Jan 22, 2013
1 parent 1bbcf0c commit 1ccc218
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 55 deletions.
1 change: 1 addition & 0 deletions library/res/values/dslv_attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<enum name="onLongPress" value="2"/>
</attr>
<attr name="drag_handle_id" format="integer" />
<attr name="fling_handle_id" format="integer" />
<attr name="click_remove_id" format="integer" />
<attr name="use_default_controller" format="boolean" />
</declare-styleable>
Expand Down
127 changes: 73 additions & 54 deletions library/src/com/mobeta/android/dslv/DragSortController.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
public static final int MISS = -1;

private int mHitPos = MISS;
private int mRemoveHitPos = MISS;
private int mFlingHitPos = MISS;

private int mClickRemoveHitPos = MISS;

Expand All @@ -82,9 +82,11 @@ public class DragSortController extends SimpleFloatViewManager implements View.O

private int mClickRemoveId;

private int mFlingHandleId;
private boolean mCanDrag;

private DragSortListView mDslv;
private int mPositionX;
private boolean mCanDrag;


/**
Expand All @@ -102,6 +104,10 @@ public DragSortController(DragSortListView dslv) {
public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitMode, int removeMode) {
this(dslv, dragHandleId, dragInitMode, removeMode, 0);
}
public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitMode, int removeMode, int clickRemoveId)
{
this(dslv, dragHandleId, dragInitMode, removeMode, clickRemoveId,0);
}

/**
* By default, sorting is enabled, and removal is disabled.
Expand All @@ -110,7 +116,8 @@ public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitM
* @param dragHandleId The resource id of the View that represents
* the drag handle in a list item.
*/
public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitMode, int removeMode, int clickRemoveId) {
public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitMode, int removeMode, int clickRemoveId, int flingHandleId)
{
super(dslv);
mDslv = dslv;
mDetector = new GestureDetector(dslv.getContext(), this);
Expand All @@ -119,6 +126,7 @@ public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitM
mTouchSlop = ViewConfiguration.get(dslv.getContext()).getScaledTouchSlop();
mDragHandleId = dragHandleId;
mClickRemoveId = clickRemoveId;
mFlingHandleId = flingHandleId;
setRemoveMode(removeMode);
setDragInitMode(dragInitMode);
mOrigFloatAlpha = dslv.getFloatAlpha();
Expand Down Expand Up @@ -186,6 +194,16 @@ public boolean isRemoveEnabled() {
*/
public void setDragHandleId(int id) {
mDragHandleId = id;
}

/**
* Set the resource id for the View that represents the fling
* handle in a list item.
*
* @param id An android resource id.
*/
public void setFlingHandleId(int id) {
mFlingHandleId = id;
}

/**
Expand Down Expand Up @@ -213,22 +231,21 @@ public void setClickRemoveId(int id) {
public boolean startDrag(int position, int deltaX, int deltaY) {

int dragFlags = 0;
if (mSortEnabled && (!mIsRemoving || mRemoveMode != FLING_OR_SLIDE_REMOVE )) {
if (mSortEnabled && !mIsRemoving) {
dragFlags |= DragSortListView.DRAG_POS_Y | DragSortListView.DRAG_NEG_Y;
//dragFlags |= DRAG_POS_Y; //for fun
}
if (mRemoveEnabled) {
if (mRemoveMode == FLING_RIGHT_REMOVE) {
dragFlags |= DragSortListView.DRAG_POS_X;
} else if (mRemoveMode == FLING_LEFT_REMOVE) {
dragFlags |= DragSortListView.DRAG_NEG_X;
}
else if( mIsRemoving && mRemoveMode == FLING_OR_SLIDE_REMOVE ){
else if( mRemoveMode == FLING_OR_SLIDE_REMOVE && mIsRemoving){
dragFlags |= DragSortListView.DRAG_POS_X;
dragFlags |= DragSortListView.DRAG_NEG_X;
}
}

mDragging = mDslv.startDrag(position - mDslv.getHeaderViewsCount(), dragFlags, deltaX, deltaY);
return mDragging;
}
Expand All @@ -253,11 +270,9 @@ public boolean onTouch(View v, MotionEvent ev) {
break;
case MotionEvent.ACTION_UP:
if (mRemoveEnabled) {
if (mIsRemoving && mRemoveMode == FLING_OR_SLIDE_REMOVE ) {
int x = mPositionX;
if( x < 0 )
x = -x;
int removePoint = mDslv.getWidth() / 3;
if ( mRemoveMode == FLING_OR_SLIDE_REMOVE && mIsRemoving ) {
int x = mPositionX >= 0 ? mPositionX : -mPositionX;
int removePoint = mDslv.getWidth() / 2;
if( x > removePoint ){
mDslv.stopDragWithVelocity(true,0);
}
Expand Down Expand Up @@ -341,8 +356,8 @@ public void onDragFloatView(View floatView, Point position, Point touch) {
public int startDragPosition(MotionEvent ev) {
return dragHandleHitPosition(ev);
}
public int startRemovePosition(MotionEvent ev) {
return removeHandleHitPosition(ev);
public int startFlingPosition(MotionEvent ev) {
return mRemoveMode == FLING_OR_SLIDE_REMOVE ? flingHandleHitPosition(ev) : MISS;
}

/**
Expand All @@ -358,8 +373,8 @@ public int startRemovePosition(MotionEvent ev) {
public int dragHandleHitPosition(MotionEvent ev) {
return viewIdHitPosition(ev, mDragHandleId);
}
public int removeHandleHitPosition(MotionEvent ev) {
return viewIdHitPosition(ev, 0);
public int flingHandleHitPosition(MotionEvent ev) {
return viewIdHitPosition(ev, mFlingHandleId);
}

public int viewIdHitPosition(MotionEvent ev, int id) {
Expand Down Expand Up @@ -408,56 +423,60 @@ public boolean onDown(MotionEvent ev) {
}

mHitPos = startDragPosition(ev);
mRemoveHitPos = MISS;
boolean startedDrag = false;
if (mHitPos != MISS && mDragInitMode == ON_DOWN ) {
startedDrag = startDrag(mHitPos, (int) ev.getX() - mItemX, (int) ev.getY() - mItemY);
}
if( !startedDrag && mRemoveMode == FLING_OR_SLIDE_REMOVE )
{
mCanDrag = true;
mPositionX = 0;
mRemoveHitPos = startRemovePosition(ev);
startDrag(mHitPos, (int) ev.getX() - mItemX, (int) ev.getY() - mItemY);
}

mIsRemoving = false;
mCanDrag = true;
mPositionX = 0;
mFlingHitPos = startFlingPosition(ev);

return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//Log.d("mobeta", "lift listener scrolled dX="+distanceX+" dY="+distanceY);

final int x1 = (int) e1.getX();
final int y1 = (int) e1.getY();
final int x2 = (int) e2.getX();
final int y2 = (int) e2.getY();
final int deltaX = x2-mItemX;
final int deltaY = y2-mItemY;

if( mRemoveMode==FLING_OR_SLIDE_REMOVE && mCanDrag && !mDragging && (mHitPos != MISS || mRemoveHitPos !=MISS )) {
final int x1 = (int) e1.getX();
final int y1 = (int) e1.getY();
final int x2 = (int) e2.getX();
final int y2 = (int) e2.getY();

boolean start = false;
if (mHitPos != MISS && mSortEnabled) {
start = Math.abs(y2 - y1) > mTouchSlop;
if( start )
mIsRemoving = false;
}
if (mRemoveEnabled && !start) {
start = Math.abs(x2 - x1) > mTouchSlop;
if( start )
mIsRemoving = true;
}
if (start) {
startDrag(mHitPos!=MISS ? mHitPos : mRemoveHitPos, x2 - mItemX, y2 - mItemY);
if( mRemoveMode==FLING_OR_SLIDE_REMOVE && mCanDrag && !mDragging && (mHitPos != MISS || mFlingHitPos !=MISS )) {

if( mHitPos != MISS )
{
if( mDragInitMode == ON_DRAG )
{
if( Math.abs(y2-y1)>mTouchSlop && mSortEnabled )
{
startDrag(mHitPos, deltaX, deltaY);
}
else if( Math.abs(x2 - x1) > mTouchSlop && mRemoveEnabled )
{
mIsRemoving = true;
startDrag(mFlingHitPos, deltaX, deltaY);
}
}
//if mDragInitMode != ON_DRAG shloudn't do anything
}
else if( Math.abs(y2-y1) > mTouchSlop)
else if( mFlingHitPos != MISS)
{
mCanDrag = false;
if( Math.abs(x2 - x1) > mTouchSlop && mRemoveEnabled)
{
mIsRemoving = true;
startDrag(mFlingHitPos, deltaX, deltaY);
}
else if( Math.abs(y2-y1) > mTouchSlop)
{
mCanDrag = false; //if started to scroll the list then don't allow sorting nor fling-removing
}
}
}
else if (mHitPos != MISS && mDragInitMode == ON_DRAG && !mDragging) {
final int x1 = (int) e1.getX();
final int y1 = (int) e1.getY();
final int x2 = (int) e2.getX();
final int y2 = (int) e2.getY();

boolean start = false;
if (mRemoveEnabled && mSortEnabled) {
Expand All @@ -469,7 +488,7 @@ else if (mHitPos != MISS && mDragInitMode == ON_DRAG && !mDragging) {
}

if (start) {
startDrag(mHitPos, x2 - mItemX, y2 - mItemY);
startDrag(mHitPos, deltaX, deltaY);
}
}
// return whatever
Expand Down Expand Up @@ -513,11 +532,10 @@ public void onShowPress(MotionEvent ev) {
@Override
public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//Log.d("mobeta", "on fling remove called");
mIsRemoving = false;
if (mRemoveEnabled) {
switch (mRemoveMode) {
case FLING_OR_SLIDE_REMOVE:
if( mIsRemoving) {
if( mIsRemoving ) {
int w = mDslv.getWidth();
int minPos = w/5;
if (velocityX > mFlingSpeed )
Expand Down Expand Up @@ -548,6 +566,7 @@ else if (velocityX < -mFlingSpeed )
break;
}
}
mIsRemoving = false;
return false;
}
};
Expand Down
5 changes: 4 additions & 1 deletion library/src/com/mobeta/android/dslv/DragSortListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ public DragSortListView(Context context, AttributeSet attrs) {
int dragHandleId = a.getResourceId(
R.styleable.DragSortListView_drag_handle_id,
0);
int flingHandleId = a.getResourceId(
R.styleable.DragSortListView_fling_handle_id,
0);
int clickRemoveId = a.getResourceId(
R.styleable.DragSortListView_click_remove_id,
0);
Expand All @@ -522,7 +525,7 @@ public DragSortListView(Context context, AttributeSet attrs) {

DragSortController controller = new DragSortController(
this, dragHandleId, dragInitMode, removeMode,
clickRemoveId);
clickRemoveId,flingHandleId);
controller.setRemoveEnabled(removeEnabled);
controller.setSortEnabled(sortEnabled);
controller.setBackgroundColor(bgColor);
Expand Down

0 comments on commit 1ccc218

Please sign in to comment.