在动画制作之前,滑动以删除卡片视图会导致短暂闪烁

Abh*_*hik 6 android android-adapter android-cursorloader android-cardview

我已经在我的应用程序中实现了cardview,并且功能向右滑动以删除.当我向右滑动卡片视图时,它会返回并返回十分之一秒,然后增益消失,导致闪烁.

我的代码对于滑动触摸侦听器是这样的.我正在更新内容解析器以及通知适配器.

SwipeableRecyclerViewTouchListener swipeTouchListener =
            new SwipeableRecyclerViewTouchListener(recyclerView,
                    new SwipeableRecyclerViewTouchListener.SwipeListener() {
                        @Override
                        public boolean canSwipe(int position) {
                            return true;
                        }

                        @Override
                        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                Post post = posts.get(position);
                                post.setIsDeleted(true);
                                getActivity().getContentResolver().update(PostsContract.PostEntry.buildUriForPost(posts.get(position).get_ID()), Utility.changePostToContentValue(post), "_id=" + post.get_ID(),null);
                                posts.remove(position);
                                adapter.notifyItemRemoved(position);

                            }

                        }
                    });
    recyclerView.addOnItemTouchListener(swipeTouchListener);
Run Code Online (Sandbox Code Playgroud)

我的onLoadFinished看起来像这样

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    if(data!=null && data.getCount() != posts.size()){
        posts.clear();
        while (data.moveToNext()){
            Post post = new Post(data);
            posts.add(post);
            adapter.notifyDataSetChanged();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

当我提出突破点.它在处理器onDismissedSwipeByRight中工作正常,其中删除的卡片不可见.但是当谈到OnLoadFinished断点时,我看到删除的卡片回来然后在函数执行后自动关闭,即使帖子Arraylist的大小完全相同.

由于删除的卡片返回十分之一秒.这会导致闪烁.谁能告诉我哪里出错了?

And*_*onu 0

从我在文档中看到的,我看到该示例也adapter.notifyDataSetChanged();像这样调用:

SwipeableRecyclerViewTouchListener swipeTouchListener =
            new SwipeableRecyclerViewTouchListener(recyclerView,
                    new SwipeableRecyclerViewTouchListener.SwipeListener() {
                        @Override
                        public boolean canSwipe(int position) {
                            return true;
                        }

                        @Override
                        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                Post post = posts.get(position);
                                post.setIsDeleted(true);
                                getActivity().getContentResolver().update(PostsContract.PostEntry.buildUriForPost(posts.get(position).get_ID()), Utility.changePostToContentValue(post), "_id=" + post.get_ID(),null);
                                posts.remove(position);
                                adapter.notifyItemRemoved(position);

                            }

                            adapter.notifyDataSetChanged();

                        }
                    });
Run Code Online (Sandbox Code Playgroud)

问题的原因可能是侦听器负责使视图向右滑动,但是一旦手势完成,它就会使视图恢复正常,以便可以重复使用。您可以尝试使视图消失,直到它被回收器视图重用(使其在适配器中可见)。