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

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

概述我有一个RecyclerView,我想展示RecyclerView有两个部分.第一部分第一部分是空的,我想把第二部分的项目拖到第一部分.将项目添加到第一部分时,应将其从第二部分删除,反之亦然. 请帮帮我朋友 试试这个,它在我的应用程序中与我合作, 在您的适配器类中,将此代码放在下面, here MessageList is name of the Arraylist, public void sw 我有一个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

总结

以上是内存溢出为你收集整理的android – 如何在Recycler视图中将项目从一个部分交换到另一个部分,反之亦然?全部内容,希望文章能够帮你解决android – 如何在Recycler视图中将项目从一个部分交换到另一个部分,反之亦然?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1128115.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存