1

我有一个列表,如果我滑动项目将从列表中删除,但我想在与从列表中删除项目(如 Gmail)相同的位置添加带有 UNDO 按钮的小吃栏。我试过了,但每次我的小吃店都在顶部。我正在使用 itemTouchHelper 类,这是我的 onSwipe 方法

 public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
    // Notify the adapter of the dismissal
   mAdapter.onItemDismiss(mRecyclerView,viewHolder,viewHolder.getAdapterPosition());

}

onItemDismiss 方法

public void onItemDismiss(final RecyclerView mRecyclerView, final  RecyclerView.ViewHolder viewHolder, final int position) {

    final String mPhoto = mItems.get(position);
    Snackbar snackbar = Snackbar
            .make(mRecyclerView, "Item Deleted from list", Snackbar.LENGTH_LONG)
            .setAction("UNDO", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // mItems.(position);
                    //notifyItemInserted(position);
                    //recyclerView.scrollToPosition(position);
                    //mItems.remove(position);
                    mItems.add(position, mPhoto);
                    notifyItemInserted(position);
                    mRecyclerView.scrollToPosition(position);
                    PhotosToDelete.remove(mPhoto);
                }
            });
    View view = snackbar.getView();
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity = Gravity.AXIS_PULL_AFTER;
    view.setLayoutParams(params);
    snackbar.show();
    mItems.remove(position);
    notifyItemRemoved(position);
    PhotosToDelete.add(mPhoto);

}
4

0 回答 0