Android RecyclerView实现点击条目删除
程序员文章站
2022-05-27 10:25:09
本文实例为大家分享了recyclerview实现点击条目删除的具体代码,供大家参考,具体内容如下
mainactivity.java
public class...
本文实例为大家分享了recyclerview实现点击条目删除的具体代码,供大家参考,具体内容如下
mainactivity.java
public class mainactivity extends appcompatactivity implements view.onclicklistener{ private button mbutton1; private button mbutton2; private button mbutton3; private button mbutton4; private button mbutton5; private recyclerview mrecyclerview; private arraylist<string> mlist; private linearlayoutmanager mlinearlayoutmanager; private rvadapter madapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); findviews(); mlist = new arraylist<>(); for (int i=0;i<20;i++){ mlist.add(i+"item"); } madapter = new rvadapter(mlist, this); mrecyclerview.setadapter(madapter); //设置分割线 mrecyclerview.additemdecoration(new divideritemdecoration(this,divideritemdecoration.vertical)); //设置默认布局 mlinearlayoutmanager = new linearlayoutmanager(this, linearlayoutmanager.vertical, false); mrecyclerview.setlayoutmanager(mlinearlayoutmanager); madapter.setonitemclicklistener(new rvadapter.onitemclicklistener() { @override public void onitemclick(int position) { madapter.remove(position); } @override public void onitemlongclick(int position) { madapter.remove(position); } }); } private void findviews() { mrecyclerview = findviewbyid(r.id.rv); mbutton1= findviewbyid(r.id.b1); mbutton2= findviewbyid(r.id.b2); mbutton3= findviewbyid(r.id.b3); mbutton4= findviewbyid(r.id.b4); mbutton5= findviewbyid(r.id.b5); mbutton1.setonclicklistener(this); mbutton2.setonclicklistener(this); mbutton3.setonclicklistener(this); mbutton4.setonclicklistener(this); mbutton5.setonclicklistener(this); } @override public void onclick(view view) { switch (view.getid()){ case r.id.b1: madapter.adddata(3); mrecyclerview.scrolltoposition(0); break; case r.id.b2: madapter.remove(mlist.size()-1); break; case r.id.b3: mlinearlayoutmanager = new linearlayoutmanager(this, linearlayoutmanager.vertical, false); mrecyclerview.setlayoutmanager(mlinearlayoutmanager); break; case r.id.b4: mrecyclerview.setlayoutmanager(new gridlayoutmanager(this, 3)); //mrecyclerview.additemdecoration(new android.support.v7.widget.divideritemdecoration(this, divideritemdecoration.horizontal)); break; case r.id.b5: mrecyclerview.setlayoutmanager(new staggeredgridlayoutmanager(2,staggeredgridlayoutmanager.vertical)); break; } } }
activity_main.xml
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="添加"/> <button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="删除"/> <button android:id="@+id/b3" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="list"/> <button android:id="@+id/b4" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="grid"/> <button android:id="@+id/b5" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="flow"/> </linearlayout> <android.support.v7.widget.recyclerview android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"/> </linearlayout> </android.support.constraint.constraintlayout>
rvadapter.java
public class rvadapter extends recyclerview.adapter<rvadapter.viewholder>{ private list<string> lists; private context mcontext; public rvadapter(list<string> lists, context context) { this.lists = lists; mcontext = context; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = view.inflate(mcontext, r.layout.item, null); viewholder holder = new viewholder(view); return holder; } public void adddata(int position) { lists.add(position,"ff"); notifyiteminserted(position); } public void remove(int i) { lists.remove(i); notifyitemremoved(i); notifydatasetchanged(); } public interface onitemclicklistener{ //自定义接口回调设置点击事件 void onitemclick(int position); void onitemlongclick(int position); } private onitemclicklistener monitemclicklistener; public void setonitemclicklistener(onitemclicklistener onitemclicklistener){ monitemclicklistener=onitemclicklistener; } @override public void onbindviewholder(final viewholder holder, final int position) { holder.mtextview.settext(lists.get(position)); holder.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int ps = holder.getlayoutposition(); monitemclicklistener.onitemclick(ps); } }); holder.itemview.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view view) { int ps=holder.getlayoutposition(); monitemclicklistener.onitemlongclick(ps); return false; } }); } @override public int getitemcount() { return lists.size(); } public static class viewholder extends recyclerview.viewholder{ public final textview mtextview; public viewholder(view itemview) { super(itemview); mtextview = (textview) itemview.findviewbyid(r.id.tv); } } }
build.gradle
implementation 'com.android.support:recyclerview-v7:27.1.1'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android RecyclerView打造悬浮效果的实现代码
-
Android 点击ImageButton时有“按下”的效果的实现
-
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
-
Android使用Recyclerview实现图片水平自动循环滚动效果
-
Android 点击生成二维码功能实现代码
-
Android scrollToTop实现点击回到顶部(兼容PullTorefreshScrollview)
-
Android实现顶部导航栏可点击可滑动效果(仿微信仿豆瓣网)
-
Android实现图片点击预览效果(zoom动画)
-
Android编程实现GridView控件点击图片变暗效果的方法
-
Android 实现左滑出现删除选项