android - SwipeListView 一次只打开一个项目

标签 android swipe ontouchlistener swipe-gesture

这个问题是指在此处找到的 SwipeListView 组件:https://github.com/47deg/android-swipelistview

在尝试了几个在网上找到的实现和修复后,我决定稍微修改源代码。

我会在此处发布此问题,因为我知道这是一个已知问题,而且我发现的所有版本最终都证明存在一些问题。

SwipeListViewTouchListener.java 发生了以下变化:

...
/**
     * Create reveal animation
     *
     * @param view      affected view
     * @param swap      If will change state. If "false" returns to the original
     *                  position
     * @param swapRight If swap is true, this parameter tells if movement is toward
     *                  right or left
     * @param position  list position
     */
    private void generateRevealAnimate(final View view, final boolean swap, final boolean swapRight, final int position) {
        int moveTo = 0;
        if (opened.get(position)) {
            if (!swap) {
                moveTo = openedRight.get(position) ? (int) (viewWidth - rightOffset) : (int) (-viewWidth + leftOffset);
            }
        } else {
            if (swap) {
                moveTo = swapRight ? (int) (viewWidth - rightOffset) : (int) (-viewWidth + leftOffset);
            }
        }
        final boolean aux = !opened.get(position);
        if(swap) {
            opened.set(position, aux);
            openedRight.set(position, swapRight);
        }

        animate(view).translationX(moveTo).setDuration(animationTime).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                swipeListView.resetScrolling();

                if (swap) {
                    if (aux) {
                        swipeListView.onOpened(position, swapRight);
                    } else {
                        swipeListView.onClosed(position, openedRight.get(position));
                    }
                }
                // if (aux || !swap) {
                // resetCell();
                // }
            }
        });
    }
...

/**
     * Close all opened items
     */

    void closeOtherOpenedItems() {
        if (opened != null && downPosition != SwipeListView.INVALID_POSITION) {
            int start = swipeListView.getFirstVisiblePosition();
            int end = swipeListView.getLastVisiblePosition();
            for (int i = start; i <= end; i++) {
                if (opened.get(i) && i != downPosition) {
                    closeAnimate(swipeListView.getChildAt(i - start).findViewById(swipeFrontView), i);
                }
            }
        }

    }
...

/**
     * @see View.OnTouchListener#onTouch(android.view.View,
     * android.view.MotionEvent)
     */
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
...
closeOtherOpenedItems();
view.onTouchEvent(motionEvent);
return true;
}

其余未提及的代码相同。

非常感谢任何评论,此更改使您不必在扩充列表的 Activity 中实现 SwipeListViewOnTouchListener

最佳答案

缺点:不会关闭由 openAnimate()

打开的行
   BaseSwipeListViewListener swipeListViewListener = new BaseSwipeListViewListener() {
    int openItem = -1;

    @Override
    public void onStartOpen(int position, int action, boolean right) {
        super.onStartOpen(position, action, right);

        if (openItem > -1)
            swipeListView.closeAnimate(openItem);

        openItem = position;
    }
   }

或者更好的方法:

 @Override
    public void onStartOpen(int position, int action, boolean right) {
        super.onStartOpen(position, action, right);
        swipeListView.closeOpenedItems();
    }

然后给listView设置监听器:

   swipeListView.setSwipeListViewListener(swipeListViewListener);

关于android - SwipeListView 一次只打开一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604293/

相关文章:

java - 无法让 OnTouchListner 工作

android - 使用布局 XML 创建按钮

android - 使用/创建 PendingIntent 的最简单方法

android - Facebook lide 滑出式菜单(不使用那个库)

android - 当键盘在 android 中弹出时,无论设备和屏幕分辨率如何,我如何改变我的观点?

java - Android 点击并按住删除项目

android - onFling 和 SimpleOnGestureListener 的问题

cocoa - 在 MAC OS X 上用 1 根手指向右/向左滑动

ios - 检测 4x4 iOS 图 block 上的触地、触地、触摸移动事件

android - 手动调用 ItemTouchHelper.onChildDraw() 以在 RecyclerView 上滑动项目