android - SwipeListview openanimate(postion)方法不起作用

标签 android animation android-listview swipe swipeview

我在 android 中使用 swipelistview,我需要在 ListView 中实现 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;
    }

请帮助我如何在单击 ListView 项时实现从左到右的滑动 View 。我为此苦苦挣扎了一个星期,但仍然没有找到好的解决方案。

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

最佳答案

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

编辑:

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

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

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

关于android - SwipeListview openanimate(postion)方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613119/

相关文章:

android - HTML5 音频标签在 Android 中不起作用

android - 为 iOS 和 Android 提供不同的 Progressive Web App UI

jquery - 有人能给我一个 jQuery 动画函数的独立代码吗

android - 具有可变级别数的 ExpandableListView

android - 使按钮占线性布局的 25%

android - 我的 fragment 没有附加到主 Activity 类。

.net - 如何每秒更新一次 WPF 绑定(bind)?

JavaFx文本移动动画

Android框架源码中启用Fast Scroll的Android Listview代码

android - android.performFiltering() 过滤时出现 sql 异常。