我需要一些帮助。我在Android中使用像Tinder应用程序的刷卡库,我正在使用交叉按钮删除卡片现在我也希望撤消选项也可以点击后退选项。任何人都可以告诉我这是用于什么方法?我真的很感激。 谢谢!
答案 0 :(得分:1)
我假设您已经实现了ÌtemTouchHelper.SimpleCallback
接口来控制滑动。我在下面列出了这样一个实现的例子:
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
final int adapterPosition = viewHolder.getAdapterPosition();
final Task taskItem = tasks.remove(adapterPosition); //tasks is an ArrayList of data that populates the RecyclerView. This line deletes an element from the ArrayList, but at the same time saves it in a variable.
adapter.notifyItemRemoved(viewHolder.getAdapterPosition()); //adapter represents my adapter class.
Snackbar snackbar = Snackbar //assuming that a Snackbar with "UNDO" button is what you want.
.make(rvTasks, "Item removed", Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View view) {
//this happens when "Undo" is clicked.
tasks.add(adapterPosition, taskItem);
adapter.notifyItemInserted(adapterPosition);
rvTasks.scrollToPosition(adapterPosition);
}
});
snackbar.show();
}
@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(rvTasks);
<强>序列:强>
final int adapterPosition = viewHolder.getAdapterPosition();
//saves the position of card/item in case we want to reinstate it.
final Task taskItem = tasks.remove(adapterPosition);
//deletes an element from the ArrayList, but at the same time saves it in a variable.
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
//adapter is notified that item is removed and thus removed from view.
然后,
Snackbar.make(... details omitted ...) //remember to add in your build.gradle: compile 'com.android.support:design:25.3.1'
tasks.add(adapterPosition, taskItem); //deleted element readded to ArrayList
adapter.notifyItemInserted(adapterPosition); //adapter notified of reinstated item
rvTasks.scrollToPosition(adapterPosition); //auto-scrolls to position where item is reinstated
答案 1 :(得分:1)