Android – how do I swap items from one part to another and vice versa in the recycler view?
•
Android
I have a recyclerview. I want to show that the recyclerview has two parts. The first part of the first part is empty. I want to drag the items of the second part to the first part. When adding items to the first part, they should be deleted from the second part, and vice versa
Please help my friend
resolvent:
Try this. It works with me in my application,
In your adapter class, put this code below,
public void swap(int firstPosition, int secondPosition)
{
Collections.swap(MessageList, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition);
}
public class MovieTouchHelper extends itemtouchhelper.SimpleCallback {
Aadapter recycleAdapter;
public MovieTouchHelper(Aadapter recycleAdapter) {
super(itemtouchhelper.UP | itemtouchhelper.DOWN, itemtouchhelper.LEFT | itemtouchhelper.RIGHT);
this.recycleAdapter = recycleAdapter;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
recycleAdapter.swap(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
recycleAdapter.remove(viewHolder.getAdapterPosition());
}
}
Then, in the mainactivity where you defined recycleview,
itemtouchhelper.Callback callback = new MovieTouchHelper(adapter);
itemtouchhelper helper = new itemtouchhelper(callback);
helper.attachToRecyclerView(rv_list);
Follow the steps below. If you find any problems or can't flip the project, please tell me directly
See this gif
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码