我有一个列表,如果我滑动项目将从列表中删除,但我想添加带有UNDO按钮的快餐栏,从列表中删除项目的位置与Gmail一样。我试过,但每次我的小吃店都在顶部。 我正在使用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);
}