Android拖放ListView问题

时间:2011-08-26 02:45:36

标签: android listview drag-and-drop commonsware-cwac

我是一名机器人初学者。 我正在做一个android项目,它有一个重新排序列表中的东西的功能。 我在https://github.com/commonsguy/cwac-touchlist#readme找到了一个开源 模块名称为CWAC:TouchListView

但在我实施期间,我遇到了一些问题,并希望有人可以帮助我,

  1. 我想在横向移动列表项时关闭删除功能,但我不能...... 如果我在TouchListView.onTouchEvent()的案例中注释删除代码MotionEvent.ACTION_CANCEL 它会发生意外行为。

  2. 另外,我想在像海豚浏览器书签页面一样拖动项目时有动画,但我不知道是不是应该实现DragListener?

  3. 但是,我修复了一个错误。

    @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);
        }
    

    对于MotionEvent.ACTION_CANCEL的情况,如果我将项目拖到list.getCount上,它将抛出异常,所以我替换条件

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

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

    然后将修复异常。

    任何人都可以帮助我吗? 非常感谢。

1 个答案:

答案 0 :(得分:0)

  

我想在横向移动列表项时关闭删除功能,但我不能

使用值为remove_mode的自定义"none"属性。例如:

<?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"
/>
  

我希望在像海豚浏览器书签页面拖动项目时有一些动画,但我不知道是不是

抱歉,我无法帮助你。