RecyclerView的Item的单击事件
程序员文章站
2022-03-26 08:59:01
显示效果 RecyclerView 的每个Item的点击事件并没有像ListView一样封装在组件中,需要Item的单击事件时就需要自己去实现,在Adapter中为RecyclerView添加单击事件参考如下: RecyclerView的使用方法请参考:RecyclerView的简单使用 第一步: ......
显示效果
recyclerview 的每个item的点击事件并没有像listview一样封装在组件中,需要item的单击事件时就需要自己去实现,在adapter中为recyclerview添加单击事件参考如下:
recyclerview的使用方法请参考:recyclerview的简单使用
第一步:
在recyclerview的adapyer中定义单击事件的回调接口:
/** * 定义recyclerview选项单击事件的回调接口 */ public interface onitemclicklistener{ //参数(父组件,当前单击的view,单击的view的位置,数据) void onitemclick(recyclerview parent,view view, int position, string data); }
第二步:
在recyclerview的adapyer中声明该接口,并提供setter方法:
private onitemclicklistener onitemclicklistener; public void setonitemclicklistener(onitemclicklistener onitemclicklistener){ this.onitemclicklistener = onitemclicklistener; }
第三步:
在recyclerview的adapyer类实现view.onclicklistener接口,并重写onclick(view view)方法,然后设置给接口的事件监听:
public class rvadapter1 extends recyclerview.adapter<rvadapter1.dataviewholder> implements view.onclicklistener{ ... view.setonclicklistener(this);//设置监听器 ... @override public void onclick(view view) { //根据recyclerview获得当前view的位置 int position = recyclerview.getchildadapterposition(view); //程序执行到此,会去执行具体实现的onitemclick()方法 if (onitemclicklistener!=null){ onitemclicklistener.onitemclick(recyclerview,view,position,mlist.get(position)); } } ... }
第四步:
在mainactivity中通过adapter设置每个item的单击事件:
adapter.setonitemclicklistener(new rvadapter1.onitemclicklistener() { @override public void onitemclick(recyclerview parent, view view, int position, string data) { toast.maketext(mainactivity.this, data, toast.length_short).show(); } });
参考代码
adapter
/** * created by jzman on 2017/5/13 0013. * recycleview的adapter */ public class rvadapter1 extends recyclerview.adapter<rvadapter1.dataviewholder> implements view.onclicklistener{ private context mcontext; private recyclerview recyclerview; private arraylist<string> mlist; public rvadapter1() {} public rvadapter1(context mcontext, arraylist<string> mlist) { this.mcontext = mcontext; this.mlist = mlist; } /** * 用于创建viewholder * @param parent * @param viewtype * @return */ @override public dataviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(mcontext).inflate(r.layout.item,null); view.setonclicklistener(this); //使用代码设置宽高(xml布局设置无效时) view.setlayoutparams(new viewgroup.layoutparams( viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content)); dataviewholder holder = new dataviewholder(view); return holder; } /** * 绑定数据 * @param holder * @param position */ @override public void onbindviewholder(dataviewholder holder, int position) { holder.tv_data.settext(mlist.get(position)); } /** * 选项总数 * @return */ @override public int getitemcount() { return mlist.size(); } @override public void onclick(view view) { //根据recyclerview获得当前view的位置 int position = recyclerview.getchildadapterposition(view); //程序执行到此,会去执行具体实现的onitemclick()方法 if (onitemclicklistener!=null){ onitemclicklistener.onitemclick(recyclerview,view,position,mlist.get(position)); } } /** * 创建viewholder */ public static class dataviewholder extends recyclerview.viewholder{ textview tv_data; public dataviewholder(view itemview) { super(itemview); tv_data = (textview) itemview.findviewbyid(r.id.tv_recycle); } } private onitemclicklistener onitemclicklistener; public void setonitemclicklistener(onitemclicklistener onitemclicklistener){ this.onitemclicklistener = onitemclicklistener; } /** * 定义recyclerview选项单击事件的回调接口 */ public interface onitemclicklistener{ //参数(父组件,当前单击的view,单击的view的位置,数据) void onitemclick(recyclerview parent,view view, int position, string data); } /** * 将recycleview附加到adapter上 */ @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); this.recyclerview= recyclerview; } /** * 将recycleview从adapter解除 */ @override public void ondetachedfromrecyclerview(recyclerview recyclerview) { super.ondetachedfromrecyclerview(recyclerview); this.recyclerview = null; } }
mainactivity
/** * created by jzman on 2017/5/13 0013. */ public class mainactivity extends appcompatactivity { private recyclerview rv; rvadapter1 adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); rv = (recyclerview) findviewbyid(r.id.rv); //设置布局管理器 // rv.setlayoutmanager(new linearlayoutmanager(this));//线性 rv.setlayoutmanager(new gridlayoutmanager(this,4));//线性 // rv.setlayoutmanager(new staggeredgridlayoutmanager(4,staggeredgridlayoutmanager.vertical));//线性 adapter = new rvadapter1(this,initdata()); adapter.setonitemclicklistener(new rvadapter1.onitemclicklistener() { @override public void onitemclick(recyclerview parent, view view, int position, string data) { toast.maketext(mainactivity.this, data, toast.length_short).show(); } }); rv.setadapter(adapter); } public static arraylist<string> initdata(){ arraylist<string> arraylist = new arraylist<>(); for (int i=0;i<50;i++){ arraylist.add("第"+i+"条数据"); } return arraylist; } }