开发者

Android Drag and Drop ListView problems

I am a android beginner. I am doing a android project which has a function to reorder something in a list. I found a open source at https://github.com/commonsguy/cwac-touchlist#readme and the module name is CWAC: TouchListView

But during my implementation, I have some problems and hope someone can help me,

  1. I wanna to turnoff the remove function when I move the list item at horizontal but I cannot... If I comment the remove code at TouchListView.onTouchEvent()'s case MotionEvent.ACTION_CANCEL It will occur the unexpected behavior.

  2. Also, I wanna have something animation during dragging the item like dolphin browser bookmark page, but I don't know is it should be implements the DragListener??

However, I have a bug fixed on it.

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mGestureDetector != null) {
            mGestureDetector.onTouchEvent(ev);
        }
        if ((mDragListener != null || mDropListener != null) && mDragView != null) {
            int action = ev.getAction();
            switch (action) {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Rect r = mTempRect;
                mDragView.getDrawingRect(r);
                stopDragging();

                if (mRemoveMode == SLIDE_RIGHT && ev.getX() > r.left + (r.width() * 3 / 4)) {
                    if (mRemoveListener != null) {
                        mRemoveListener.remove(mFirstDragPos);
                    }
                    unExpandViews(true);
                } else if (mRemoveMode == SLIDE_LEFT && ev.getX() < r.left + (r.width() / 4)) {
                    if (mRemoveListener != null) {
                        mRemoveListener.remove(mFirstDragPos);                      
                    }
                    unExpandViews(true);
                } else {
                    if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) {
                        mDropListener.drop(mFirstDragPos, mDragPos);
                    }
                    unExpandViews(false);
                }
                break;

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                int x = (int) ev.getX();
                int y = (int) ev.getY();
                dragView(x, y);
                int itemnum = getItemForPosition(y);
                if (itemnum >= 0) {
                    if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) {
                        if (mDragListener != null) {
                            mDragListener.drag(mDragPos, itemnum);
                        }
                        mDragPos = itemnum;
                        doExpansion();
                    }
                    int speed = 0;
                    adjustScrollBounds(y);
                    if (y > mLowerBound) {
                        // scroll the list up a bit
                        speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4;
                    } else if (y < mUpperBound) {
                        // scroll the list down a bit
                        speed = y < mUpperBound / 2 ? -16 : -4;
                    }
                    if (speed != 0) {
                        int ref = pointToPosition(0, mHeight / 2);
                        if (ref == AdapterView.INVALID_POSITION) {
                            // we hit a divider or an invisible view, check
                            // somewhere else
                            ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64);
                        }
                        View v = getChildAt(ref - getFirstVisiblePosition());
                        if (v != null) {
                            int pos = v.getTop();
                            setSelectionFromTop(ref, pos - speed);                          
                        }
                    }
                }
                break;
            }
            return true;
        }
        return super.onTouchEvent(ev);
    }

For the case MotionEvent.ACTION_CANCEL, if I drag the item over the list.getCount, it will throw exception, so I replace the condition

from

if (mDropListener != null && 开发者_运维知识库mDragPos >= 0 && mDragPos < getCount() ) {
      mDropListener.drop(mFirstDragPos, mDragPos);
}

to

if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) {
      mDropListener.drop(mFirstDragPos, mDragPos);
}

Then the exception will be fixed.

Could anyone can help me?? Many thanks.


I wanna to turnoff the remove function when I move the list item at horizontal but I cannot

Use the custom remove_mode attribute with a value of "none". For example:

<?xml version="1.0" encoding="utf-8"?>
<com.commonsware.cwac.tlv.TouchListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tlv="http://schemas.android.com/apk/res/com.commonsware.cwac.tlv.demo"

    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    tlv:normal_height="64dip"
    tlv:grabber="@+id/icon"
    tlv:remove_mode="none"
/>

I wanna have something animation during dragging the item like dolphin browser bookmark page, but I don't know is it

I will not be able to help you with that, sorry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜