Android RecyclerView仿新闻头条的频道管理功能
程序员文章站
2023-11-29 08:02:33
需要在build里添加依赖
compile 'com.android.support:recyclerview-v7:25.3.1'
布局文件activi...
需要在build里添加依赖
compile 'com.android.support:recyclerview-v7:25.3.1'
布局文件activity_main
<android.support.v7.widget.recyclerview android:layout_weight="1" android:id="@+id/recyclerview_up" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.recyclerview> <!--用来划分上下的频道--> <view android:layout_width="match_parent" android:layout_height="1dp" android:background="#999999"> </view> <android.support.v7.widget.recyclerview android:layout_weight="1" android:id="@+id/recyclerview_down" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.recyclerview>
在layout下定义一个message.xml布局文件,用来显示recyclerview里的文字,跟listview的意义差不多
<textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:padding="3dp" android:text="123123123" />
写一个 myitemonclicklistener 接口用来监听点击事件,在里边传入两个参数,第一个是试图,第二是点击的下标
public interface myitemonclicklistener { void onclick(view view,int position); }
recyclerview的适配器类
//recyclerview.adapter<?>里写的是优化继承viewholder的帮助类 public class viewadapter extends recyclerview.adapter<viewadapter.myviewholder> { private context mcontext; /** *特别注意一下mlist后面一定要默认赋值,不然运行的时候会报错,因为我们默认上面的recyclerview是没有添加数据的 */ private list<string> mlist=new arraylist<>(); private myitemonclicklistener myitemonclicklistener; public viewadapter(context context) { this.mcontext = context; } //传数组的方法 public void setmessage(list<string> list) { this.mlist = list; } //写个方法把接口传过来 public void setmyitemonclicklistener(myitemonclicklistener myitemonclicklistener) { this.myitemonclicklistener = myitemonclicklistener; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { //打气筒填充我们自定义的message.xml的试图 view view = view.inflate(mcontext, r.layout.messgae, null); //优化 final myviewholder holder = new myviewholder(view); //监听 view.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //调用我们自定义接口的方法 myitemonclicklistener.onclick(v, holder.getlayoutposition()); } }); return holder; } @override public void onbindviewholder(myviewholder holder, int position) { myviewholder viewholder = holder; viewholder.textview.settext(mlist.get(position)); } @override public int getitemcount() { return mlist.size(); } //viewholder优化 static class myviewholder extends recyclerview.viewholder { private textview textview; public myviewholder(view itemview) { super(itemview); textview = (textview) itemview.findviewbyid(r.id.textview); } } }
在mainactivity的实现
public class mainactivity extends appcompatactivity { private recyclerview recyclerview_up; private recyclerview recyclerview_down; private list<string> listup = new arraylist<>(); private list<string> listdown = new arraylist<>(); private viewadapter adapterup; private viewadapter adapterdown; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //添加数据 initdata(); initview(); } //添加数据 for (int i = 0; i < 15; i++) { //默认的我们只给下面的recyclerview添加了数据 listdown.add("频道管理" + i); } } private void initview() { recyclerview_up = (recyclerview) findviewbyid(r.id.recyclerview_up); recyclerview_down = (recyclerview) findviewbyid(r.id.recyclerview_down); initup(); initdown(); } private void initup() { //创建适配器 adapterup = new viewadapter(mainactivity.this); //添加数据,我们默认创建的时候是空的 adapterup.setmessage(listup); recyclerview_up.setadapter(adapterup); //recyclerview显示样式 gridlayoutmanager gridlayoutmanagerup = new gridlayoutmanager(this, 3); recyclerview_up.setlayoutmanager(gridlayoutmanagerup); //点击监听 adapterup.setmyitemonclicklistener(new myitemonclicklistener() { @override public void onclick(view view, int position) { log.e("up", "上" + position); //下面的recyclerview添加当前点击的条目信息并刷新数组 listdown.add(listup.get(position)); adapterdown.setmessage(listdown); adapterdown.notifydatasetchanged(); //从当前数组移除数据并刷新数组 listup.remove(position); adapterup.setmessage(listup); adapterup.notifydatasetchanged(); } }); } private void initdown() { adapterdown = new viewadapter(mainactivity.this); adapterdown.setmessage(listdown); recyclerview_down.setadapter(adapterdown); gridlayoutmanager gridlayoutmanagerdown = new gridlayoutmanager(this, 3); recyclerview_down.setlayoutmanager(gridlayoutmanagerdown); adapterdown.setmyitemonclicklistener(new myitemonclicklistener() { @override public void onclick(view view, int position) { log.e("down", "下" + position); //下面的recyclerview添加当前点击的条目信息并刷新数组 listup.add(listdown.get(position)); adapterup.setmessage(listup); adapterup.notifydatasetchanged(); //从当前数组移除数据并刷新数组 listdown.remove(position); adapterdown.setmessage(listdown); adapterdown.notifydatasetchanged(); } }); } }
以上所述是小编给大家介绍的android recyclerview仿新闻头条的频道管理功能,希望对大家有所帮助