GithubHelp home page GithubHelp logo

b7woreo / various Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 130 KB

RecyclerView#Adapter创建工具,支持灵活的列表Adapter创建并复用ViewHolder逻辑

License: MIT License

Kotlin 96.30% Shell 3.70%

various's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.