SwipeListView一次只打开一个项目

时间:2014-03-24 08:27:55

标签: 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;
}

未提及的其余代码是相同的。

任何评论都非常感谢,这些更改可以防止您在导致列表膨胀的活动中实施SwipeListViewOnTouchListener

5 个答案:

答案 0 :(得分:3)

缺点:没有关闭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);

答案 1 :(得分:2)

您的修复工作正常,但有一种方法可以在不影响原始代码的情况下执行此操作:

swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
    int openItem = -1;
    int lastOpenedItem = -1;
    int lastClosedItem = -1;

    @Override
    public void onOpened(int position, boolean toRight) {
        lastOpenedItem = position;
        if (openItem > -1 && lastOpenedItem != lastClosedItem) {
            swipeListView.closeAnimate(openItem);
        }
        openItem = position;
    }

    @Override
    public void onStartClose(int position, boolean right) {
        Log.d("swipe", String.format("onStartClose %d", position));
        lastClosedItem = position;
    }
}

但是,您应该发送拉取请求以应用您的代码,因为这样可以修复错误。

来源:https://github.com/47deg/android-swipelistview/issues/46

答案 2 :(得分:1)

如果您要修改swipelistview库本身,我有一个更简单的解决方案。

SwipeListViewTouchListener.java

开头的onTouch方法中,将以下if块添加到case MotionEvent.ACTION_DOWN:
if(lastOpenedPosition != downPosition && opened.get(lastOpenedPosition)) {
    closeAnimate(lastOpenedPosition);
    return false;
}

创建int lastOpenedPosition字段并将其初始化为0,并在generateRevealAnimate块内的if (aux)方法中添加:

lastOpenedPosition = position;

我还会将配置变量(res/values/swipelistview_attrs.xml)添加到SwipeListView并将其添加到onTouch if块,以添加关闭此功能的功能。 这基本上导致在行打开时触摸列表,而行将关闭。其中,imho,只有在你打开另一行后才能关闭行。

答案 3 :(得分:1)

swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//...
  @Override
  public void onClickBackView(int position) {
    //DELETE ITEM
    adapter.notifyDataSetChanged();
    swipeListView.closeOpenedItems();
  }
//...
});

答案 4 :(得分:0)

是的,原始代码的SwipeListView可以同时打开多个项目。您的代码段可以一次打开一个项目吗?或者当打开另一个项目时,打开的项目将被关闭?