Various databinding support library

General Android RecyclerView#Adapter for reuse view holder.

License

License

Categories

Categories

Data
GroupId

GroupId

com.chrnie
ArtifactId

ArtifactId

various-databinding
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

Various databinding support library
General Android RecyclerView#Adapter for reuse view holder.
Project URL

Project URL

https://github.com/chrnie233/various
Source Code Management

Source Code Management

https://github.com/chrnie233/various

Download various-databinding

How to add to project

<!-- https://jarcasting.com/artifacts/com.chrnie/various-databinding/ -->
<dependency>
    <groupId>com.chrnie</groupId>
    <artifactId>various-databinding</artifactId>
    <version>1.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.chrnie/various-databinding/
implementation 'com.chrnie:various-databinding:1.0.0'
// https://jarcasting.com/artifacts/com.chrnie/various-databinding/
implementation ("com.chrnie:various-databinding:1.0.0")
'com.chrnie:various-databinding:aar:1.0.0'
<dependency org="com.chrnie" name="various-databinding" rev="1.0.0">
  <artifact name="various-databinding" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.chrnie', module='various-databinding', version='1.0.0')
)
libraryDependencies += "com.chrnie" % "various-databinding" % "1.0.0"
[com.chrnie/various-databinding "1.0.0"]

Dependencies

compile (5)

Group / Artifact Type Version
com.android.databinding : baseLibrary jar 3.1.3
com.android.databinding » library jar 3.1.3
com.android.databinding » adapters jar 3.1.3
various » various jar unspecified
com.android.support » recyclerview-v7 jar 27.1.1

Project Modules

There are no modules declared in this project.

various

这是一个用来创建 RecyclerView.Adapter 的工具类,它着重要于完成数据到 ViewHolder 的映射,以便捷的方式创建多类型 Adapter、复用 Item 逻辑。

基础用法

1. 添加依赖

implementation 'com.chrnie:various:1.0.0'

2. 编写视图数据类型和 ViewBinder

每个Item都有一个对应的数据类型,例如一条新闻:

class NewsVO(
  val title: String,
  val brief: String
) {
  fun onClick() {
    // do something on click
  }
}

每种数据类型至少有一个相对应的 ViewBinder,ViewBinder API 接口类似于 Adapter。

class NewsViewBinder() : ViewBinder<NewsVO, NewsViewBinder.NewsViewHolder>() {

  override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): NewsViewHolder {
    val view = inflater.inflate(R.layout.item_news, parent, false)
    return NewsViewHolder(view)
  }

  override fun onBindViewHolder(holder: NewsViewHolder, data: NewsVO, payloads: List<Any>) {
    holder.title.text = data.title
    holder.brief.text = data.brief
    holder.itemView.setOnClickListener { data.onClick() }
  }

  class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    val title: TextView by lazy { itemView.findViewById<TextView>(R.id.tvTitle) }
    val brief: TextView by lazy { itemView.findViewById<TextView>(R.id.tvBrief) }
  }
}

这样 Item 中的逻辑全部转移到了 ViewBinder 中,可以在多个列表间轻松复用。

3. 创建Adapter

private val adapter: Various<NewsVO> by lazy {
    Various.Builder<NewsVO>()
      .register(NewsVO::class, NewsViewBinder())
      .build()
}

4. 添加要显示的数据

adapter.dataList = arrayListOf(
    NewsVO("第一条新闻", "第一条新闻简介"),
    NewsVO("第二条新闻", "第二条新闻简介"),
    NewsVO("第三条新闻", "第三条新闻简介"),
    NewsVO("第四条新闻", "第四条新闻简介")
)
adapter.notifyDataSetChanged()

使用 Lambda

当一个 Item 不需要复用逻辑,并且只简单的创建和绑定视图,那么可以通过使用 Lambda 快速完成。

private val adapter: Various<NewsVO> by lazy {
    Various.Builder<NewsVO>(CustomItemMatcherFactory())
      .register(
        NewsVO::class,
        { inflater, parent ->
          val view = inflater.inflate(R.layout.item_news, parent, false)
          NewsViewHolder(view)
        }, { holder, data, _ ->
          holder.title.text = data.title
          holder.brief.text = data.brief
          holder.itemView.setOnClickListener { data.onClick() }
        }
    ).build()

定制 ItemMatcher

如果对默认的 ItemMatcher 不满意,那么可以实现 ItemMatcher 及 ItemMatcher.Factory 接口并在创建 Various 时将其作为参数传入。

private val adapter: Various<NewsVO> by lazy {
    Various.Builder<NewsVO>(CustomItemMatcherFactory())
        .register(NewsVO::class, NewsViewBinder())
        .build()
}

License

MIT License

Versions

Version
1.0.0