详解分别用Kotlin和java写RecyclerView的示例
程序员文章站
2023-12-17 16:01:16
本文介绍了分别用kotlin和java写recyclerview的示例,分享给大家,具体如下:
java:跟一般的写法一样,增加了按钮响应
mainactivi...
本文介绍了分别用kotlin和java写recyclerview的示例,分享给大家,具体如下:
java:跟一般的写法一样,增加了按钮响应
mainactivity:
public class mainactivity extends appcompatactivity implements recycleradapter.onitemclicklistener{ private recyclerview mrecyclerview; private recyclerview.layoutmanager mlayoutmanager; private recycleradapter mrecycleradapter; private arraylistlist; @override protected void oncreate(bundlesaved instancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recycleritem item; list = new arraylist<>(); item = new recycleritem(); item.setname("no."+0); list.add(item); mrecyclerview = findviewbyid(r.id.recyclerview); mrecycleradapter = new recycleradapter(this,list); mrecycleradapter.setonitemclicklistener(this); // 竖直方向的网格样式,每行一个item mlayoutmanager=new gridlayoutmanager(this,3,orientationhelper.vertical,false); // 设置布局管理器 mrecyclerview.setlayoutmanager(mlayoutmanager); // 设置adapter mrecyclerview.setadapter(mrecycleradapter); // 设置item添加和移除的动画 mrecyclerview.setitemanimator(newdefaultitemanimator()); } //adapter的按钮点击事件 @override public void onitemcontentclick(viewview,intposition) { //点击最后一个按钮增加一个item if(position == list.size()-1) { recycleritemitem=newrecycleritem(); item.setname("no."+(position+1)); list.add(0,item); //更新recyclerview mrecycleradapter.updatedata(list); }else{ toast.maketext(this,"no."+position,toast.length_short).show(); } } }
adapter:
public class recycleradapter extends recyclerview.adapter{ private arraylist mdata; private context mcontext; private onitemclicklistener onitemclicklistener; public void setonitemclicklistener(onitemclicklistener listener) { this.onitemclicklistener = listener; } public recycleradapter(context mcontext,arraylist mdata){ this.mdata = mdata; this.mcontext = mcontext; } //刷新整个list public void updatedata(arraylist data) { this.mdata = data; notifydatasetchanged(); } //刷新局部list public void updatedataitem(arraylist data, int itemnm) { this.mdata = data; notifyitemchanged(itemnm); } @override public view holder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.recycler_item, parent, false); viewholder viewholder = new viewholder(v); return viewholder; } @override public void onbindviewholder(final viewholder holder, int position) { holder.btn.settext(mdata.get(position).getname()); holder.btn.setonclicklistener(newview.onclicklistener() { @override public void onclick(view v) { if(onitemclicklistener != null) { int pos = holder.getlayoutposition(); onitemclicklistener.onitemcontentclick(holder.itemview, pos); } } }); } @override public int getitemcount() { return mdata == null ? 0 : mdata.size(); } public static class viewholder extends recyclerview.viewholder{ button btn; public viewholder(view itemview) { super(itemview); btn = itemview.findviewbyid(r.id.recycle_name); } } public interface onitemclicklistener{ void onitemcontentclick(view view, int position); } }
recycleritem:
public class recycleritem{ string name; public string getname() { return name; } public void setname(string name) { this.name = name; }
activity_main:
<android.support.v7.widget.recyclerview xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tudou.recycleviewdemo.mainactivity"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:paddingbottom="5dip"> </android.support.v7.widget.recyclerview> </android.support.constraint.constraintlayout>
recycler_item:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp"> <button android:id="@+id/recycle_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="no.1"/> </relativelayout>
kotlin:语法略有不同,recyclerview调用类似
mainactivity:
class mainactivity : appcompatactivity(), mainadapter.itemclick{ val items : mutablelist = arraylist() var mainadapter : mainadapter ?= null override fun oncreate(savedinstancestate:bundle?) { super.oncreate(savedinstancestate) setcontentview(r.layout.activity_main) //添加一个item var item : recycleritem item = recycleritem() item.name = "no."+0 items.add(item) val recyclerview = findviewbyid(r.id.recyclerview) as recyclerview recyclerview.layoutmanager = gridlayoutmanager(this, 3, orientationhelper.vertical, false) mainadapter=mainadapter(items, this) mainadapter!!.setitemclicklistener(this) recyclerview.adapter = mainadapter recyclerview.itemanimator = defaultitemanimator() } //adapter的按钮点击事件 override fun onitemclick(v : view, position : int) { if(position == items.size - 1) { val item = recycleritem() item.name = "no." + (position + 1) items.add(0, item) //更新数据 mainadapter!!.updatedata(items) }else{ toast.maketext(this,items.get(position).name, toast.length_short).show() } } }
mainadapter:
class mainadapter : recyclerview.adapter{ var items : list? = null var context : context ?= null var itemclick : itemclick ?= null constructor(items : list, context : context){ this.items = items this.context = context } fun setitemclicklistener(itemclick : itemclick){ this.itemclick = itemclick } fun updatedata(items : list){ this.items = items notifydatasetchanged() } override fun onbindviewholder(holder : viewholder?, position : int) { var name=items!!.get(position).name if(holder != null) { holder.textname.settext(name) holder.textname.setonclicklistener(view.onclicklistener{ if(itemclick != null) { itemclick!!.onitemclick(holder.itemview, position) } }) } } override fun getitemcount():int{ return items!!.size } override fun oncreateviewholder(parent : viewgroup?, viewtype : int) : viewholder{ val v = layoutinflater.from(parent!!.context).inflate(r.layout.recycler_item, parent, false) as relativelayout val holder = viewholder(v) return holder } class viewholder(itemview : view?) : recyclerview.viewholder(itemview) { var textname : textview = itemview!!.findviewbyid(r.id.recycle_name) as textview } interface itemclick{ fun onitemclick(v : view, position : int); } }
recycleritem:
class recycleritem{ var name : string = "" get set }
activity_main:
<android.support.constraint.constraintlayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tudo.kotlintdemo.mainactivity"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:paddingbottom="5dip"> </android.support.v7.widget.recyclerview> </android.support.constraint.constraintlayout>
recycler_item:
<relativelayout> xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp"> <button android:id="@+id/recycle_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="no.1"/> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读