Recyclerview添加头布局和尾布局、item点击事件详解
程序员文章站
2023-10-19 22:15:00
简介:
本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件
思路:
主要重写recyclerview.adapt...
简介:
本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件
思路:
主要重写recyclerview.adapter中的一些方法
1.public int getitemcount() item熟练 +2(头布局和尾布局)
2.public int getitemviewtype(int position) 判断position 设置itemtype
3.创建不同的viewholder,分别用来加载头布局,正常布局,尾布局
4.public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) 根据viewtype的不同返回不同的viewholder
5.public void onbindviewholder(recyclerview.viewholder holder, int position) 根据holder的不同绑定不同的数据
案例:
主布局中只有一个recyclerview,里面的item除了基本的两种类型,还有头布局和尾布局,点击基本类型跳转到另外一个activity,显示图片
下面主要贴出adapter和mainactivity中的代码,其他的布局文件,类比较简单就不贴出了
1.adapter
public class myadapter extends recyclerview.adapter<recyclerview.viewholder> implements view.onclicklistener { private context context; private list<person> datas; private static final int item_one=1; private static final int item_two=2; private static final int item_head=3; private static final int item_foot=4; private int headviewcount=1; private int footviewcount=1; private onitemclicklistener onitemclicklistener; public myadapter(context context, list<person> datas) { this.context = context; this.datas = datas; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { if(viewtype==item_head){ view view= layoutinflater.from(context).inflate(r.layout.item_head,parent,false); return new headerviewholder(view); }else if(viewtype==item_foot){ view view=layoutinflater.from(context).inflate(r.layout.item_foot,parent,false); return new footviewholder(view); } else if(viewtype==item_one){ view view= layoutinflater.from(context).inflate(r.layout.item_recyclerview,parent,false); return new viewholder(view); }else if(viewtype==item_two){ view view=layoutinflater.from(context).inflate(r.layout.item_recyclerview2,parent,false); return new viewholder(view); } return null; } @override public void onbindviewholder(recyclerview.viewholder holder, final int position) { if(holder instanceof headerviewholder){ }else if(holder instanceof footviewholder){ } else if(holder instanceof viewholder){ //注意除去头布局 holder.itemview.settag(position-1); holder.itemview.setonclicklistener(this); ((viewholder)holder).img.setbackgroundresource(datas.get(position-1).getimgid()); ((viewholder)holder).tv1.settext(datas.get(position-1).getname()); ((viewholder)holder).tv2.settext(datas.get(position-1).getdetail()); } } @override public int getitemcount() { if(datas==null){ return headviewcount+footviewcount; } return datas.size()+headviewcount+footviewcount; } @override public int getitemviewtype(int position) { if(isheadview(position)){ return item_head; } if(isfootview(position)){ return item_foot; } if(position%2==0){ return item_one; }else{ return item_two; } } public boolean isheadview(int position){ return headviewcount!=0&&position<headviewcount; } public boolean isfootview(int position){ return footviewcount!=0&&position>=(datas.size()+headviewcount); } @override public void onclick(view view) { if(onitemclicklistener!=null){ onitemclicklistener.onitemclick(view, (integer) view.gettag()); } } class viewholder extends recyclerview.viewholder{ textview tv1,tv2; imageview img; public viewholder(view itemview) { super(itemview); tv1=itemview.findviewbyid(r.id.tv1); tv2=itemview.findviewbyid(r.id.tv2); img=itemview.findviewbyid(r.id.img); } } class headerviewholder extends recyclerview.viewholder{ public headerviewholder(view itemview) { super(itemview); } } class footviewholder extends recyclerview.viewholder{ public footviewholder(view itemview) { super(itemview); } } public void setonitemclicklistener(onitemclicklistener onitemclicklistener){ this.onitemclicklistener=onitemclicklistener; } interface onitemclicklistener{ void onitemclick(view view,int position); } }
2.mainactivity
public class mainactivity extends appcompatactivity implements myadapter.onitemclicklistener { private recyclerview recyclerview; private list<person> datas; private myadapter adapter; private int[] imgs={r.drawable.p1,r.drawable.p2,r.drawable.p3,r.drawable.p4,r.drawable.p5,r.drawable.pic1, r.drawable.pic2,r.drawable.pic3,r.drawable.pic4,r.drawable.pic5,r.drawable.pic6}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview= (recyclerview) findviewbyid(r.id.recyclerview); initdata(); initrecyclerview(); } private void initrecyclerview() { recyclerview.setlayoutmanager(new linearlayoutmanager(this)); adapter=new myadapter(this,datas); adapter.setonitemclicklistener(this); recyclerview.setadapter(adapter); } private void initdata() { datas=new arraylist<>(); for(int i=0;i<imgs.length;i++){ person p=new person(); p.setname("hahfa"+i); p.setdetail("今天是星期一,还有5天才能休息"+i); p.setimgid(imgs[i]); datas.add(p); } } @override public void onitemclick(view view, int position) { intent intent=new intent(mainactivity.this,imageactivity.class); intent.putextra("imgid",datas.get(position).getimgid()); startactivity(intent); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。