Android recycleView的应用和点击事件实例详解
程序员文章站
2024-02-23 10:52:22
android recycleview的应用和点击事件
效果图:
长按是删除:
下面代码:
mainactivity 类
public class...
android recycleview的应用和点击事件
效果图:
长按是删除:
下面代码:
mainactivity 类
public class mainactivity extends appcompatactivity { recyclerview rv; list<string> mlist = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); rv = (recyclerview) findviewbyid(r.id.rv); for (int i = 0; i < 1000; i++) { mlist.add("条目" + i); } final myadapter adapter = new myadapter(this, mlist); adapter.setonitemclicklistener(new myadapter.onitemclicklistener() { @override public void onclick(int position) { toast.maketext(getbasecontext(), "点击的是" + position + "的itemclick", toast.length_short).show(); } }); adapter.setonitemlongclicklistener(new myadapter.onitemlongclicklistener() { @override public void onlongclick(int position) { //长按删除数据 mlist.remove(position); adapter.notifydatasetchanged(); } }); rv.setadapter(adapter); //布局管理器 rv.setlayoutmanager(new staggeredgridlayoutmanager(5, staggeredgridlayoutmanager.vertical)); } public static class vh extends recyclerview.viewholder { textview tv; public vh(view itemview) { super(itemview); tv = (textview) itemview.findviewbyid(android.r.id.text1); tv.setbackgroundresource(r.drawable.bg); } } }
myadapter.java类
public class myadapter extends recyclerview.adapter<mainactivity.vh> { context mcontext; list<string> mlist; public myadapter(context context, list<string> list) { this.mcontext = context; this.mlist = list; } @override public mainactivity.vh oncreateviewholder(viewgroup parent, int viewtype) { return new mainactivity.vh(view.inflate(mcontext, android.r.layout.simple_list_item_1, null)); } @override public void onbindviewholder(mainactivity.vh holder, final int position) { holder.tv.settext(mlist.get(position)); //自己做item点击 holder.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (onitemclicklistener != null) onitemclicklistener.onclick(position); } }); holder.itemview.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { if (onitemlongclicklistener != null) onitemlongclicklistener.onlongclick(position); //返回false会在长安结束后继续点击 return true; } }); } @override public int getitemcount() { return mlist.size(); } /** * 点击 */ public interface onitemclicklistener { void onclick(int position); } onitemclicklistener onitemclicklistener; public void setonitemclicklistener(onitemclicklistener onitemclicklistener) { this.onitemclicklistener = onitemclicklistener; } public interface onitemlongclicklistener { void onlongclick(int position); } onitemlongclicklistener onitemlongclicklistener; public void setonitemlongclicklistener(onitemlongclicklistener onitemlongclicklistener) { this.onitemlongclicklistener = onitemlongclicklistener; } }
activity_main.xml类
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.lesson10_recyclerview2.mainactivity"> <android.support.v7.widget.recyclerview android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Android 图片选择详解及实例代码
推荐阅读
-
Android recycleView的应用和点击事件实例详解
-
Android 日期和时间的使用实例详解
-
实例详解Android Selector和Shape的用法
-
C#中委托和事件在观察者模式中的应用实例
-
详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法
-
Android TextView Marquee的应用实例详解
-
实例详解Android Selector和Shape的用法
-
Android style的继承方式 点(.)和parent详解及实例
-
详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法
-
详解iOS中Button按钮的状态和点击事件