其他分享
首页 > 其他分享> > android – 如何在Recycler视图中将项目从一个部分交换到另一个部分,反之亦然?

android – 如何在Recycler视图中将项目从一个部分交换到另一个部分,反之亦然?

作者:互联网

我有一个RecyclerView,我想展示RecyclerView有两个部分.第一部分第一部分是空的,我想把第二部分的项目拖到第一部分.将项目添加到第一部分时,应将其从第二部分删除,反之亦然.

请帮帮我朋友RecyclerView

解决方法:

试试这个,它在我的应用程序中与我合作,

在您的适配器类中,将此代码放在下面,

here MessageList is name of the Arraylist,

public void swap(int firstPosition, int secondPosition)
{
    Collections.swap(MessageList, firstPosition, secondPosition);
    notifyItemMoved(firstPosition, secondPosition);
}  

now add this small class separately,

here Adapter is name of the Adapter class put your adapter name

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());
    }
}

然后在你定义了recycleview的mainActivity中,

ItemTouchHelper.Callback callback = new MovieTouchHelper(adapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(rv_list);

here rv_list is name of recycleview.

按照以下步骤操作,如果您发现任何问题或者无法翻转项目,请直接告诉我……

看到这个GIF

enter image description here

标签:android,xml,android-recyclerview,recycler-adapter,notifydatasetchanged
来源: https://codeday.me/bug/20190628/1310613.html