如何在recyclerView中按下后退按钮恢复滑动视图?
我的代码
RecyclerListAdapter.java
public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder>
implements ItemTouchHelperAdapter {
private final List<Card> mItems = new ArrayList<>();
Card car1 = new Card(R.drawable.card1, "Text First");
Card car2 = new Card(R.drawable.card1, "Text Second");
Card car3 = new Card(R.drawable.card3, "Text First");
Card car4 = new Card(R.drawable.card4, "Text Second");
Card car5 = new Card(R.drawable.card3, "Text First");
Card car6 = new Card(R.drawable.card2, "Text Second");
public RecyclerListAdapter() {
mItems.addAll(Arrays.asList(car1, car2,car3,car4,car5,car6));
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
final Card card = mItems.get(position);
holder.textView.setText(card.getText());
holder.imageView.setImageResource(card.getImage());
}
@Override
public void onItemDismiss(int position) {
mItems.remove(position);
notifyItemRemoved(position);
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
Card prev = mItems.remove(fromPosition);
mItems.add(toPosition > fromPosition ? toPosition - 1 : toPosition, prev);
notifyItemMoved(fromPosition, toPosition);
}
@Override
public int getItemCount() {
return mItems.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements
ItemTouchHelperViewHolder {
public final TextView textView;
public final ImageView imageView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.heading_text);
imageView= (ImageView)itemView.findViewById(R.id.image);
}
@Override
public void onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY);
}
@Override
public void onItemClear() {
itemView.setBackgroundColor(0);
}
}
}
SimpleItemTouchHelperCallback.java
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter mAdapter;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter mAdapter) {
this.mAdapter = mAdapter;
}
@Override
public boolean isLongPressDragEnabled() {
return false;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = 0;
int swipeFlags = ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
public void onSelectedChange(RecyclerView.ViewHolder viewHolder, int actionstate) {
if (actionstate != ItemTouchHelper.ACTION_STATE_IDLE) {
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
super.onSelectedChanged(viewHolder, actionstate);
}
@Override
public void clearView(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder){
super.clearView(recyclerView,viewHolder);
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}
ItemTouchHelperAdapter.java
public interface ItemTouchHelperAdapter {
void onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
}
ItemTouchHelperViewHolder.java
public interface ItemTouchHelperViewHolder {
void onItemSelected();
void onItemClear();
}
一切正常但我需要知道如何在按下后退按钮时重新滑动视图。
答案 0 :(得分:0)
在OnItemDismiss
方法中,保存要删除的项目的副本,然后创建一个OnItemRestore
方法,将其放回适配器。
从活动的OnBackPressed
方法调用新方法
private Object lastRemovedItem;
private int lastIndex=-1;
@Override
public void onItemDismiss(int position) {
lastRemovedItem=mItems.get(position);
lastindex=position;
mItems.remove(position);
notifyItemRemoved(position);
}
public void restoreLastItem(){
if (lastIndex==-1) return;
mItems.add(lastindex,lastRemovedItem);
notifyItemInserted(lastIndex);
lastRemovedItem=null;
lastIndex=-1;
}