Android中RecyclerView上拉下拉,分割线,多条目的实例代码
程序员文章站
2024-03-31 11:17:10
//activity的xml
//activity的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="baway.com.cj.myrecyclerview2.mainactivity"> <android.support.v4.widget.swiperefreshlayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_above="@+id/progress" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.recyclerview> </android.support.v4.widget.swiperefreshlayout> <progressbar android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:visibility="gone" android:layout_alignparentbottom="true"/> </relativelayout> item1的xml和item2的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" android:background="#f00000" tools:context="baway.com.cj.myrecyclerview2.mainactivity"> <textview android:id="@+id/name" android:layout_width="match_parent" android:layout_height="50dp" android:textsize="20sp" android:gravity="center" android:textcolor="#00ff00"/> </relativelayout> item2 <?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" android:background="#f000ff" tools:context="baway.com.cj.myrecyclerview2.mainactivity"> <textview android:id="@+id/name" android:layout_width="match_parent" android:layout_height="50dp" android:textsize="20sp" android:gravity="center" android:textcolor="#00ff00"/> </relativelayout> //activity,有上啦下拉,添加分割线, package baway.com.cj.myrecyclerview2; import android.graphics.color; import android.os.handler; import android.support.v4.widget.swiperefreshlayout; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.defaultitemanimator; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.view; import android.widget.progressbar; import android.widget.toast; import java.util.arraylist; public class mainactivity extends appcompatactivity { private recyclerview recyclerview; private mainadapter mainadapter; private swiperefreshlayout swiperefreshlayout; private linearlayoutmanager linearlayoutmanager; private progressbar progress; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); initdata(); } private void initview() { recyclerview = (recyclerview)findviewbyid(r.id.recyclerview); swiperefreshlayout = (swiperefreshlayout) findviewbyid(r.id.swiperefreshlayout); progress = (progressbar)findviewbyid(r.id.progress); linearlayoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(linearlayoutmanager); mainadapter = new mainadapter(this); mainadapter.setonclicklistener(new myitemonclicklistener() { @override public void onitemclick(view view, int position) { toast.maketext(mainactivity.this, " "+position, toast.length_short).show(); } }); recyclerview.setadapter(mainadapter); recyclerview.additemdecoration(new myitemdecoration()); recyclerview.setitemanimator(new defaultitemanimator()); swiperefreshlayout.setcolorschemecolors(color.red,color.green,color.blue); swiperefreshlayout.setprogressviewoffset(true,0,200); swiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { new handler().postdelayed(new runnable() { @override public void run() { swiperefreshlayout.setrefreshing(false); } },3000); } }); recyclerview.addonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); int lastvisibleitemposition = linearlayoutmanager.findlastvisibleitemposition(); int itemcount = linearlayoutmanager.getitemcount(); if (lastvisibleitemposition + 1 >= itemcount && dy > 0) { progress.setvisibility(view.visible); new handler().postdelayed(new runnable() { @override public void run() { progress.setvisibility(view.gone); } },3000); } } @override public void onscrollstatechanged(recyclerview recyclerview, int newstate) { super.onscrollstatechanged(recyclerview, newstate); } }); } private void initdata() { arraylist<string> strings = new arraylist<>(); for (int i = 0; i < 50; i++) { strings.add("name "+i); } mainadapter.setdata(strings); mainadapter.notifydatasetchanged(); } } //adpter有多条目展示 package baway.com.cj.myrecyclerview2; import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.util.arraylist; /** * created by cj on 2017/1/12. */ public class mainadapter extends recyclerview.adapter { private context context; private arraylist<string> datas = new arraylist<>(); private myitemonclicklistener myitemonclicklistener; private final int type_0 = 0; private final int type_1 = 1; public mainadapter(context context) { this.context = context; } @override public int getitemviewtype(int position) { if (position %3 == 0) { return type_0; } else { return type_1; } } public void setonclicklistener(myitemonclicklistener onclicklistener) { this.myitemonclicklistener = onclicklistener; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { if (viewtype == type_0) { view inflate = layoutinflater.from(context).inflate(r.layout.item_main, null); return new myviewholder(inflate); } else { view inflate = layoutinflater.from(context).inflate(r.layout.item_main2, null); return new myviewholder2(inflate); } } @override public void onbindviewholder(recyclerview.viewholder holder, final int position) { if (getitemviewtype(position) == type_0) { myviewholder myviewholder = (myviewholder)holder; myviewholder.name.settext(datas.get(position)); myviewholder.name.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { myitemonclicklistener.onitemclick(v,position); } }); } else { myviewholder2 myviewholder = (myviewholder2)holder; myviewholder.name.settext(datas.get(position)); myviewholder.name.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { myitemonclicklistener.onitemclick(v,position); } }); } } @override public int getitemcount() { return datas.size(); } public void setdata(arraylist<string> data) { if (data == null) { return; } this.datas.addall(data); } public class myviewholder extends recyclerview.viewholder{ public textview name; public myviewholder(view itemview) { super(itemview); name = (textview) itemview.findviewbyid(r.id.name); } } public class myviewholder2 extends recyclerview.viewholder{ public textview name; public myviewholder2(view itemview) { super(itemview); name = (textview) itemview.findviewbyid(r.id.name); } } } //自定义分割线继承系统的 recyclerview.itemdecoration package baway.com.cj.myrecyclerview2; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.view; /** * created by cj on 2017/1/12. */ public class myitemdecoration extends recyclerview.itemdecoration { private paint paint; public myitemdecoration() { paint = new paint(); paint.setcolor(color.blue); } @override public void ondraw(canvas c, recyclerview parent, recyclerview.state state) { super.ondraw(c, parent, state); //c.drawcircle(300,300,300,paint); int childcount = parent.getchildcount(); for (int i = 0; i < childcount; i++) { view childat = parent.getchildat(i); c.drawrect(childat.getleft(),childat.getbottom(),childat.getwidth()+childat.getleft(),childat.getbottom()+5,paint); } } @override public void ondrawover(canvas c, recyclerview parent, recyclerview.state state) { super.ondrawover(c, parent, state); // int childcount = parent.getchildcount(); // for (int i = 0; i < childcount; i++) { // view childat = parent.getchildat(i); // c.drawrect(childat.getleft(),childat.gettop(),childat.getwidth()+childat.getleft(),childat.getbottom()+5,paint); // } } @override public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) { super.getitemoffsets(outrect, view, parent, state); outrect.set(20,0,20,5); } }
以上所述是小编给大家介绍的android中recyclerview上拉下拉,分割线,多条目的实例代码,希望对大家有所帮助
下一篇: _blank开新窗口不符合标准?