文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android MVVM架构实现RecyclerView列表详解流程

2024-04-02 19:55

关注

效果图

导入引用

导入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;
    }
    
    public void setmName(String mName) {
        this.mName = mName;
        notifyPropertyChanged( BR.mName );
    }
    @Bindable
    public List<Information> getInformationList() {
        return informationList;
    }

    
    public void setmSex(String mSex) {
        this.mSex = mSex;
        notifyPropertyChanged( BR.mSex );
    }
   
    public void setmAge(String mAge) {
        this.mAge = mAge;
        notifyPropertyChanged( BR.mAge );
    }
    
    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列表内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯