隐藏(并重新显示)由Cursor

时间:2015-09-04 20:31:20

标签: android android-recyclerview android-cursoradapter

我有一个带有CursorAdapter的RecyclerView,如下所示:https://gist.github.com/skyfishjy/443b7448f59be978bc59

我还有一个以这种方式实现的回收器附加的ItemTouchHelper(不介意我的Snackbar修改):

private class TouchHelperCallbacks extends ItemTouchHelper.SimpleCallback {

        public TouchHelperCallbacks(int dragDirs, int swipeDirs) {
            super(dragDirs, swipeDirs);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                              RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
            Snackbar.make(recycler,
                          R.string.course_hidden,
                          Snackbar.LENGTH_LONG)
                    .setOnHideListener(new Snackbar.OnHideListener() {
                        @Override
                        public void onHide() {
                            //after Snackbar disappears, either because of a 
                            //timeout or it is swiped away, remove item from db 
                        }
                    })
                    .setAction(R.string.undo,
                               new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       //put card back into the recycler
                                   }
                               })
                    .setActionTextColor(getActivity().getResources().getColor(R.color.color_accent))
                    .colorTheFuckingTextToWhite(getActivity())
                    .doStuffThatGoogleDidntFuckingDoProperly(getActivity(), null)
                    .show();
        }
    }

卡被正确地擦掉了,但是在它下方的卡片抬起时,只有空白区域留下了曾经刷过的卡片。如果我在Snackbar显示时调用notifyDatasetChanged(),它将重新出现在之前的位置。

我想避免访问数据库并使undo尽可能轻量级。我可以在Snackbar仍然显示时实现一个额外的列或删除/插入行,但对于这么简单的任务来说它似乎太重了(几秒钟就会有3-4次查询)。

理想情况下,我想访问当前在屏幕上的ViewHolders,并按照所需的高度进行翻译,如果用户选择撤消,则执行相同操作,但方向相反,然后将卡插回。

viewHolder.itemView.setVisibility(View.GONE)似乎没有任何效果。

1 个答案:

答案 0 :(得分:1)

https://github.com/brnunes/SwipeableRecyclerView处的代码完全符合您的要求。

这是我实施它的方式:

private RecyclerView rvRoutes;
(...)

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rvRoutes = (RecyclerView) view.findViewById(R.id.rvRoutes);
           (...)

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

                                @Override
                                public boolean canSwipeRight(int position) {   return true;  }

                                @Override
                                public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
                                    for (int position : reverseSortedPositions) {
                                        Toast.makeText(MainActivity.this, mItems.get(position) + " swiped left", Toast.LENGTH_SHORT).show();


                                    }
                                }

                                @Override
                                public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                                    for (int position : reverseSortedPositions) {
                                        Toast.makeText(MainActivity.this, mItems.get(position) + " swiped right", Toast.LENGTH_SHORT).show();

                                    }
                                    adapter.notifyDataSetChanged();
                                }
                            });

            rvRoutes.addOnItemTouchListener(swipeTouchListener);

}

这就是所有需要的,因为所有动画都是向上,向下移动项目都在SwipeableRecyclerViewTouchListener类中。