千家信息网

Android MVVM架构怎么实现RecyclerView列表

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本篇内容主要讲解"Android MVVM架构怎么实现RecyclerView列表",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android MVVM架
千家信息网最后更新 2025年01月31日Android MVVM架构怎么实现RecyclerView列表

本篇内容主要讲解"Android MVVM架构怎么实现RecyclerView列表",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android MVVM架构怎么实现RecyclerView列表"吧!

    效果图

    导入引用

    导入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 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 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 informationList) {        this.informationList = informationList;        notifyPropertyChanged( BR.informationList );    }}

    将视图层与数据层绑定

    @{information.mName}

    建立RecyclerView子项

    导入实体类

                 
                                                    

    适配器

    建立适配器

    引入数据源

    public MyRecyclerView(List 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{    private List informationList;    private OnItemClickListener mOnItemClickListener;    public MyRecyclerView(List 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 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列表"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

    0