跳转至

概览

version Min SDK Version JDK Version GitHub license

VastAdapter 提供了便于 RecyclerView 使用的通用适配器(Adapter)。其主要框架如下:

VastAdapter 架构

VastAdapter 架构
  • Adapter

    适配器(Adapter)提供了 VastAdapterVastListAdapterVastPagingAdapter 以及针对数据绑定场景下的 VastBindAdapterVastBindListAdapterVastBindPagingAdapter

  • ItemWrapper

    数据项包装器(ItemWrapper)用于为适配器(Adapter)提供布局资源 id 以及列表项点击事件。其目的在于分离数据项与布局,点击事件。

  • Data

    列表内展示的数据项。

  • ItemHolder 和 ItemBindHolder

  • ItemDiffUtil

    用于解决适配器获取到的是 ItemWrapper 而非数据项本身的问题。

快速开始

Version 1.1.1

下面以展示 Person 数据为例:

  • Person 定义

    data class Person(val name: String, val sentence: String)
    
  • item_person 定义

    person 布局文件
    <layout>
        <data>
            <variable name="person" type="com.ave.vastgui.app.adapter.entity.Person" />
        </data>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/item_textview_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">
            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:text="@{person.name}"
                android:textSize="18sp"
                android:textStyle="bold" />
            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/sentence"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:text="@{person.sentence}" />
        </LinearLayout>
    </layout>
    
  • 使用 VastBindAdapter 作为适配器

    getBinding().personRv.layoutManager = LinearLayoutManager(this)
    getBinding().personRv.adapter =
        VastBindAdapter(this, BR.person, ArrayList<ItemWrapper<Person>>().apply {
            repeat(20) {
                add(ItemWrapper(Person("第${it}名", "我是第${it}名"), R.layout.item_person))
            }
        })
    

VastAdapter 架构