Android 拖放ListView问题

发布于 2024-12-01 15:19:24 字数 4376 浏览 3 评论 0原文

我是一名安卓初学者。 我正在做一个 android 项目,它有一个功能可以对列表中的某些内容重新排序。 我在 https://github.com/commonsguy/cwac-touchlist#readme 模块名称是 CWAC: TouchListView

但是在我的实现过程中,我遇到了一些问题,希望有人可以帮助我,

  1. 我想在水平移动列表项时关闭删除功能,但我不能... 如果我在 TouchListView.onTouchEvent() 的情况下评论删除代码 MotionEvent.ACTION_CANCEL 它将发生意外的行为。

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

不过,我修复了一个错误。

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

然后异常将被修复。

有人可以帮助我吗? 非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在我安 2024-12-08 15:19:24

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

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

<?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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文