美文网首页
我所搭建的MVVM设计模式的Android框架(八)

我所搭建的MVVM设计模式的Android框架(八)

作者: 欧西里 | 来源:发表于2020-11-19 11:36 被阅读0次

最近找到了适配器布局实现MVVM架构的方法,有大佬写的第三方很好用,可以免除写适配器的烦恼,用的时间不是很长,暂时没遇到什么问题。
BindingCollectionAdapter
一、使用方法:
1.导入依赖

implementation 'me.tatarka.bindingcollectionadapter2:bindingcollectionadapter:4.0.0'
implementation 'me.tatarka.bindingcollectionadapter2:bindingcollectionadapter-recyclerview:4.0.0'
implementation 'me.tatarka.bindingcollectionadapter2:bindingcollectionadapter-viewpager2:4.0.0'

2.添加属性:

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:itemBinding="@{demoViewModel.itemBinding}"
        app:items="@{demoViewModel.items}"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

3.编写item布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="com.demo.mvvm.entity.DemoEntity" />

        <variable
            name="demoEntity"
            type="DemoEntity" />

    </data>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:text="@={demoEntity.username}" />
</layout>

4.viewmodel中添加双向绑定

    //recycleview
    public final ObservableList<DemoEntity> items = new ObservableArrayList<>();
    public final ItemBinding<DemoEntity> itemBinding = ItemBinding.of(BR.demoEntity, R.layout.item);

以上为BindingCollectionAdapter简单用法,可以去大佬的github上看看,写的很详细。地址:https://github.com/evant/binding-collection-adapter
二、后记
经过几个项目的监测,搭建的框架基本上可以满足日常所需了,于是把代码整理了一下上传到了github上,地址:https://github.com/yinchangxu/MVVMDemo
这个系列到第八篇就结束了。以后可能会修改这八篇文章中不对或不好的地方,不会出现第九篇了。
另外github上的demo应该会长期维护,觉得缺少什么示例的可以在这留言给我,看到了我会及时补充进去的。

相关文章

网友评论

      本文标题:我所搭建的MVVM设计模式的Android框架(八)

      本文链接:https://www.haomeiwen.com/subject/gxmkiktx.html