banner
jzman

jzman

Coding、思考、自觉。
github

MultiTypeオープンソースプロジェクトのソースコード解析

PS: The day before yesterday, I had something to do, so I specifically went to the university to take a look. Familiar scenes, unfamiliar people. It's been a few years, and I can never go back.

First, let's take a look at the introduction of the project author. MultiType is an intermediate dispatch framework for multi-type list views. It can help you quickly and clearly develop some complex list pages. It is data-driven and supports one-to-one and one-to-many relationships between layout types and view binders. It is oriented towards interface programming. MultiType delegates the work of creating views and filling data to ItemViewBinder. ItemViewBinder corresponds to the data type T to be filled and the ViewHolder. Subsequent different data types need to implement the corresponding ItemViewBinder. The project address is as follows:

We will learn the source code of MultiType from the following aspects:

  1. Usage of MultiTypeAdapter
  2. Registration process of MultiTypeAdapter
  3. ViewHolder and data filling of MultiTypeAdapter

Usage of MultiTypeAdapter#

Let's take a look at the difference in writing between MultiTypeAdapter and a regular Adapter, as shown below:

// base
adapter.register(TextItemViewDelegate())
adapter.register(ImageItemViewDelegate())
adapter.register(RichItemViewDelegate())
// One to many
adapter.register(Data::class).to(
  DataType1ViewDelegate(),
  DataType2ViewDelegate()
).withKotlinClassLinker { _, data ->
  when (data.type) {
    Data.TYPE_2 -> DataType2ViewDelegate::class
    else -> DataType1ViewDelegate::class
  }
}
adapter.items = items
recyclerView.adapter = adapter
adapter.notifyDataSetChanged()

I won't go into details here, you can check the address above for more information.

Registration process of MultiTypeAdapter#

The registration process of MultiTypeAdapter mainly adds the Type to the corresponding collection of MultiTypeAdapter for later use. The calling process is as follows:

Mermaid Loading...

Let's take a look at the registration process of one-to-many relationship in MultiTypeAdapter:

Mermaid Loading...

As shown above, it eventually returns to the registration process of one-to-one.

ViewHolder and data filling of MultiTypeAdapter#

In the previous section, MultiTypeAdapter delegates some methods of RecyclerView.Adapter to the specific implementation of ItemViewBinder, and adds the specific ItemViewBinder, Linker, and Class information of data type T to the type collection in MultiTypeAdapter in the form of Type. The item type, ViewHolder creation, and data filling all need to be implemented by the specific implementation class of ItemViewBinder. Let's explore this process in detail.

getItemViewType#

getItemViewType is used to return the view type of the item. In MultiTypeAdapter, the view type index returned by getItemViewType is the index of the corresponding Type in MutableTypes. The calling process is as follows:

Mermaid Loading...

From the above, it can be seen that the final index of the view type returned is index + linker.index. From the source code, it can be seen that the DefaultLinker is used in the one-to-one case, and its default index is 0, so the final index of the view type returned is the index of the corresponding Type in MutableTypes.

onCreateViewHolder#

onCreateViewHolder is used to create a ViewHolder based on the layout file. The specific calling process is as follows:

Mermaid Loading...

onBindViewHolder#

onBindViewHolder is similar to onCreateViewHolder, both of which obtain the corresponding delegated ItemViewDelete from the collection of added types.

In summary, the above usage allows the types of displayed items to be extracted as ItemViewBinder, reducing the coupling between different types of items. It allows adding item types at any time without modifying the Adapter, just by adding new implementations of ItemViewBinder.

The "one-to-many" relationship mentioned by the author mainly refers to multiple layout implementations corresponding to the same item type. It mainly learns the author's code ideas and is oriented towards interface programming. Personally, I understand that MultiTypeAdapter is an extension of RecyclerView.Adapter rather than an encapsulation. It can be further encapsulated for personal use.

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。