1

我正在使用 android 中的 swipelistview,我需要在列表视图中实现 onClick 以刷出列表项。我在 OnclickfrontView 中使用了 openanimate 方法。但我的输出结果是相反的。我从右到左而不是从左到右获得视图。我使用的库是https://github.com/47deg/android-swipelistview. swipelistview 的 xml 布局是

android:id="@+id/search_screen_listview_sharedData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:cacheColorHint="#00000000"
        android:background="@color/white"
        android:divider="@color/sliding_menu_listview_divider"
        android:dividerHeight="0.2dp"
        android:fadingEdgeLength="0dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:listSelector="@color/transparent"
        swipe:swipeFrontView="@+id/front"
        swipe:swipeBackView="@+id/back"
        swipe:swipeOpenOnLongPress="false"
        swipe:swipeActionLeft="reveal"
        swipe:swipeActionRight="reveal"
        swipe:swipeCloseAllItemsWhenMoveList="true"
        swipe:swipeDrawableChecked="@color/sliding_menu_selection_blue"
        swipe:swipeDrawableUnchecked="@color/white"
        swipe:swipeOffsetLeft="200dp"
        swipe:swipeOffsetRight="70dp"
        swipe:swipeMode="right" />   


供您参考的示例代码是

swipeListView = (SwipeListView) view
                .findViewById(R.id.listview_sharedData);
        swipeFrontView = (View) view.findViewById(R.id.front);
        if (Build.VERSION.SDK_INT >= 11) {
            swipeListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            swipeListView
                    .setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

                        @Override
                        public void onItemCheckedStateChanged(ActionMode mode,
                                int position, long id, boolean checked) {
                            mode.setTitle("Selected ("
                                    + swipeListView.getCountSelected() + ")");
                        }

                        @Override
                        public void onDestroyActionMode(ActionMode mode) {
                            swipeListView.unselectedChoiceStates();
                        }

                        @Override
                        public boolean onActionItemClicked(ActionMode mode,
                                android.view.MenuItem item) {
                            // TODO Auto-generated method stub
                            return false;
                        }

                        @Override
                        public boolean onCreateActionMode(ActionMode mode,
                                android.view.Menu menu) {
                            // TODO Auto-generated method stub
                            return false;
                        }

                        @Override
                        public boolean onPrepareActionMode(ActionMode mode,
                                android.view.Menu menu) {
                            // TODO Auto-generated method stub
                            return false;
                        }
                    });
        }

        swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
            @Override
            public void onOpened(int position, boolean toRight) {
            }

            @Override
            public void onClosed(int position, boolean fromRight) {
            }

            @Override
            public void onListChanged() {
            }

            @Override
            public void onMove(int position, float x) {
            }


            @Override
            public void onStartOpen(int position, int action, boolean right) {
                Log.d("swipe", String.format("onStartOpen %d - action %d",
                        position, action));
            }

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

            @Override
            public void onClickFrontView(int position) {
                Log.d("swipe", String.format("onClickFrontView %d", position));

                swipeListView.openAnimate(position);

            }

            @Override
            public void onClickBackView(int position) {
                Log.d("swipe", String.format("onClickBackView %d", position));
                //swipeListView.closeAnimate(position);

            }

            @Override
            public void onDismiss(int[] reverseSortedPositions) {
                /*
                 * for (int position : reverseSortedPositions) {
                 * data.remove(position); } adapter.notifyDataSetChanged();
                 */
            }

        });

        if (Constant.List != null) {
            if (Constant.List.size() > 0) {
                swipeListView.setAdapter(new CustomContentListAdapter(
                        getActivity(), Constant.List, true));

            }
        }

        return view;
    }

当我单击列表视图项目时,请帮助我如何实现从左到右的 swipview。我为此苦苦挣扎了一周,但仍然没有得到好的解决方案。

public void openAnimate(int position) {
        touchListener.openAnimate(position);

    }

   protected void openAnimate(int position) {
        openAnimate(
                swipeListView.getChildAt(
                        position - swipeListView.getFirstVisiblePosition())
                        .findViewById(swipeFrontView), position);
    }

   private void openAnimate(View view, int position) {
        if (!opened.get(position)) {
            generateRevealAnimate(view, true, false, 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);
            }
        }

        animate(view).translationX(moveTo).setDuration(animationTime)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        swipeListView.resetScrolling();
                        if (swap) {
                            boolean aux = !opened.get(position);
                            opened.set(position, aux);
                            if (aux) {
                                swipeListView.onOpened(position, swapRight);
                                openedRight.set(position, swapRight);
                            } else {
                                swipeListView.onClosed(position,
                                        openedRight.get(position));
                            }
                        }
                        resetCell();
                    }
                });
    }

    private void resetCell() {
        if (downPosition != ListView.INVALID_POSITION) {
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
                backView.setVisibility(View.VISIBLE);
            }
            frontView.setClickable(opened.get(downPosition));
            frontView.setLongClickable(opened.get(downPosition));
            frontView = null;
            backView = null;
            downPosition = ListView.INVALID_POSITION;
        }
    }
4

4 回答 4

1

编辑openAnimate()您下载的库的源代码中的方法,使其向另一个方向移动(假设包含源代码)

编辑:

下面是确定将视图移动到何处的代码

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

因为我没有完整的代码,所以我不知道什么viewWidthrightOffsetleftOffset被定义为。我建议你尝试这样的事情:

: (int) (-viewWidth + leftOffset);

: (int) (viewWidth - leftOffset);

对这两种情况都这样做。我不能保证这会起作用,但如果不起作用,请尝试使用这些值。

于 2014-02-06T20:27:46.023 回答
1

您需要添加一些代码:

SwipeListView.java

public void openAnimateRight(int position) {
    touchListener.openAnimateRight(position);
}

public void closeAnimateRight(int position) {
    touchListener.closeAnimateRight(position);
}

SwipeListViewTouchListener.java

protected void openAnimateRight(int position) {
    openAnimateRight(swipeListView.getChildAt(position - swipeListView.getFirstVisiblePosition()).findViewById(swipeFrontView), position);
}
protected void closeAnimateRight(int position) {
    closeAnimateRight(swipeListView.getChildAt(position - swipeListView.getFirstVisiblePosition()).findViewById(swipeFrontView), position);
}

private void openAnimateRight(View view, int position) {
    if (!opened.get(position)) {
        generateRevealAnimate(view, true, true, position);
    }
}
private void closeAnimateRight(View view, int position) {
    if (opened.get(position)) {
        generateRevealAnimate(view, true, true, position);
    }
}

然后你可以打电话:

@Override
public void onClickFrontView(int position) {
    Log.d("swipe", String.format("onClickFrontView %d", position));

    swipe_list_view.openAnimateRight(position);

}

这对我有用:)

于 2014-05-04T01:11:08.960 回答
0

在搜索您提供的存储库时。库本身提供对 swipetoright 模式的支持。

公共最终静态 int SWIPE_MODE_RIGHT = 2;

https://github.com/47deg/android-swipelistview/blob/20dbffdf3417a7ad0cbba06cc6f1caa7ffb3808d/swipelistview/src/com/fortysevendeg/swipelistview/SwipeListView.java

尝试该选项来完成您的任务...

一切顺利..

于 2014-02-18T14:40:36.217 回答
0

就像在 OnclickBackView 中一样,您必须编写 swipeListView.closeanimate(postion);

于 2014-02-08T19:49:42.887 回答