Android MVVM架构实现RecyclerView列表详解流程
程序员文章站
2022-03-25 23:35:21
目录效果图导入引用导入recyclerview依赖导入databinding引用代码解析建立实体类建立recyclerview子项适配器建立适配器设置子项点击事件adapter全部代码建立vm层子项点...
效果图
导入引用
导入recyclerview依赖
implementation 'androidx.recyclerview:recyclerview:1.1.0'
导入databinding引用
databinding { enabled = true }
代码解析
建立实体类
继承被观察者模式baseobservable
@bindable绑定字段
刷新数据 notifypropertychanged( br.mname );//br自动生成
public class information extends baseobservable { public string mname; public string msex; public string mage; public list<information> informationlist; public information(){ } public information(string mname,string msex,string mage){ this.mname = mname; this.msex = msex; this.mage = mage; } @bindable public string getmage() { return mage; } @bindable public string getmname() { return mname; } @bindable public string getmsex() { return msex; } /** * @param mname*/ public void setmname(string mname) { this.mname = mname; notifypropertychanged( br.mname ); } @bindable public list<information> getinformationlist() { return informationlist; } /** * @param msex */ public void setmsex(string msex) { this.msex = msex; notifypropertychanged( br.msex ); } /** * @param mage */ public void setmage(string mage) { this.mage = mage; notifypropertychanged( br.mage ); } /** * @param informationlist */ public void setinformationlist(list<information> informationlist) { this.informationlist = informationlist; notifypropertychanged( br.informationlist ); } }
将视图层与数据层绑定
@{information.mname}
建立recyclerview子项
导入实体类
<data> <variable name="information" type="com.franzliszt.mvvmrecyclerview.model.information" /> </data>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="information" type="com.franzliszt.mvvmrecyclerview.model.information" /> </data> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="30dp" android:layout_marginright="30dp" android:background="#f5f5f5"> <linearlayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:layout_margintop="20dp" android:background="@drawable/item_bg_style" android:gravity="center" android:paddingleft="20dp"> <textview android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="名字" android:text="@{information.mname}"/> <textview android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="性别" android:text="@{information.msex}"/> <textview android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="年龄" android:text="@{information.mage}"/> </linearlayout> </linearlayout> </layout>
适配器
建立适配器
引入数据源
public myrecyclerview(list<information> informationlist){ this.informationlist = informationlist; }
插入布局文件
@nonnull @override public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) { viewdatabinding binding = databindingutil.inflate( layoutinflater.from( parent.getcontext()) , r.layout.recyclerview_item,parent,false); return new viewholder( binding,monitemclicklistener ); }
设置数据
@override public void onbindviewholder(@nonnull viewholder holder, int position) { information information = informationlist.get( position ); holder.binding.setvariable( br.information,informationlist.get( position )); holder.binding.executependingbindings(); }
自定义viewholder类
class viewholder extends recyclerview.viewholder implements view.onclicklistener{ viewdatabinding binding = null; onitemclicklistener listener; public viewholder(@nonnull viewdatabinding binding,onitemclicklistener listener) { super( binding.getroot() ); this.listener = listener; this.binding = binding; binding.getroot().setonclicklistener( this ); } public viewdatabinding getbinding(){ return binding; }
设置子项点击事件
定义接口
public interface onitemclicklistener { void onitemclick(view view, int position); }
继承view.onclicklistener点击事件
class viewholder extends recyclerview.viewholder implements view.onclicklistener
获取接口实例
public viewholder(@nonnull viewdatabinding binding,onitemclicklistener listener)
调用接口
@override public void onclick(view v) { if (listener != null){ listener.onitemclick( v,getposition() ); } }
开放接口
public void setonitemclicklistener(onitemclicklistener listener) { this.monitemclicklistener = listener; }
adapter全部代码
public class myrecyclerview extends recyclerview.adapter<myrecyclerview.viewholder>{ private list<information> informationlist; private onitemclicklistener monitemclicklistener; public myrecyclerview(list<information> informationlist){ this.informationlist = informationlist; } public interface onitemclicklistener { void onitemclick(view view, int position); } public void setonitemclicklistener(onitemclicklistener listener) { this.monitemclicklistener = listener; } @nonnull @override public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) { viewdatabinding binding = databindingutil.inflate( layoutinflater.from( parent.getcontext()) , r.layout.recyclerview_item,parent,false); return new viewholder( binding,monitemclicklistener ); } @override public void onbindviewholder(@nonnull viewholder holder, int position) { information information = informationlist.get( position ); holder.binding.setvariable( br.information,informationlist.get( position )); holder.binding.executependingbindings(); } @override public int getitemcount() { return informationlist.size(); } class viewholder extends recyclerview.viewholder implements view.onclicklistener{ viewdatabinding binding = null; onitemclicklistener listener; public viewholder(@nonnull viewdatabinding binding,onitemclicklistener listener) { super( binding.getroot() ); this.listener = listener; this.binding = binding; binding.getroot().setonclicklistener( this ); } public viewdatabinding getbinding(){ return binding; } @override public void onclick(view v) { if (listener != null){ listener.onitemclick( v,getposition() ); } } } }
建立vm层
子项点击事件的使用
实现onitemclicklistener接口
@override public void initlistener(){ adapter.setonitemclicklistener( new myrecyclerview.onitemclicklistener() { @override public void onitemclick(view view, int position) { toast.maketext( context,"位数:"+position,toast.length_short ).show(); } } ); }
vm层代码
public class viewmodel implements iviewmodel{ public information information; public list<information> mlist = new arraylist<>( ); private recyclerview recyclerview; private context context; private myrecyclerview adapter; public viewmodel(information information, recyclerview recyclerview, context context){ this.information = information; this.recyclerview = recyclerview; this.context = context; initrecyclerview(); initdata(); initlistener(); } private void initrecyclerview(){ recyclerview.setlayoutmanager( new linearlayoutmanager( context ) ); adapter = new myrecyclerview( mlist ); recyclerview.setadapter( adapter ); } @override public void initdata() { for (int i = 0; i < 10; i++) { mlist.add( new information( "张三"+i,"男",i+"" ) ); log.d( "tag",mlist.get( i ).mname+"" ); } information.setinformationlist( mlist ); } @override public void initlistener(){ adapter.setonitemclicklistener( new myrecyclerview.onitemclicklistener() { @override public void onitemclick(view view, int position) { toast.maketext( context,"位数:"+position,toast.length_short ).show(); } } ); } }
数据与视图交互
public class mainactivity extends appcompatactivity { private activitymainbinding binding; @override protected void oncreate(bundle savedinstancestate) { super.oncreate( savedinstancestate ); binding = databindingutil.setcontentview( this,r.layout.activity_main ); binding.setviewmodel( new viewmodel(new information( ),binding.myrecyclerview,mainactivity.this) ); } }
到此这篇关于android mvvm架构实现recyclerview列表详解流程的文章就介绍到这了,更多相关android recyclerview列表内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!