/
AssemblyRecyclerAdapter.kt
148 lines (127 loc) · 5.9 KB
/
AssemblyRecyclerAdapter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (C) 2021 panpf <panpfpanpf@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.panpf.assemblyadapter.recycler
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.panpf.assemblyadapter.AssemblyAdapter
import com.github.panpf.assemblyadapter.Item
import com.github.panpf.assemblyadapter.ItemFactory
import com.github.panpf.assemblyadapter.ItemId
import com.github.panpf.assemblyadapter.Placeholder
import com.github.panpf.assemblyadapter.internal.ItemDataStorage
import com.github.panpf.assemblyadapter.internal.ItemFactoryStorage
import com.github.panpf.assemblyadapter.recycler.internal.RecyclerViewHolderWrapper
/**
* An implementation of [RecyclerView.Adapter], which implements multi-type adapters through standardized [ItemFactory].
* [AssemblyRecyclerAdapter] will use the data corresponding to position to find a matching [ItemFactory] (cannot find an exception will be thrown),
* and then use [ItemFactory] to create an item view and bind the data
*
* @param itemFactoryList The collection of [ItemFactory] passed in from outside, cannot be empty.
* Each type of data in the data set must have a matching [ItemFactory], otherwise an exception will be thrown
* @param initDataList Initial data set
* @see ItemFactory
*/
open class AssemblyRecyclerAdapter<DATA>(
itemFactoryList: List<ItemFactory<out Any>>,
initDataList: List<DATA>? = null,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), AssemblyAdapter<DATA, ItemFactory<out Any>> {
private val itemFactoryStorage = ItemFactoryStorage(
itemFactoryList, "ItemFactory", "AssemblyRecyclerAdapter", "itemFactoryList"
)
private val itemDataStorage = ItemDataStorage(initDataList) { oldList, newList ->
onDataListChanged(oldList, newList)
}
private var parent: RecyclerView? = null
/**
* Get the current list. If a null list is submitted through [submitList], or no list is submitted, an empty list will be returned.
* The returned list may not change-changes to the content must be passed through [submitList].
*/
val currentList: List<DATA>
get() = itemDataStorage.readOnlyList
init {
require(itemFactoryList.isNotEmpty()) { "itemFactoryList Can not be empty" }
}
/**
* Set the new list to be displayed.
*/
open fun submitList(list: List<DATA>?) {
itemDataStorage.submitList(list)
}
override fun getItemCount(): Int {
return itemDataStorage.dataCount
}
fun getItemData(position: Int): DATA {
return itemDataStorage.getData(position)
}
override fun getItemId(position: Int): Long {
return if (hasStableIds()) {
val data = getItemData(position) ?: Placeholder
if (data is ItemId) data.itemId else data.hashCode().toLong()
} else {
RecyclerView.NO_ID
}
}
override fun getItemViewType(position: Int): Int {
val data = getItemData(position) ?: Placeholder
return itemFactoryStorage.getItemTypeByData(data)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val itemFactory = itemFactoryStorage.getItemFactoryByItemType(viewType)
val item = itemFactory.dispatchCreateItem(parent)
return RecyclerViewHolderWrapper(item)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is RecyclerViewHolderWrapper<*>) {
@Suppress("UNCHECKED_CAST")
val item = holder.wrappedItem as Item<Any>
val data = getItemData(position) ?: Placeholder
val absoluteAdapterPosition =
holder.absoluteAdapterPosition.takeIf { it != -1 } ?: holder.position
item.dispatchBindData(position, absoluteAdapterPosition, data)
val parent = parent
val layoutManager = parent?.layoutManager
if (layoutManager is StaggeredGridLayoutManager && layoutManager is FullSpanSupport) {
val layoutParams =
holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
layoutParams.isFullSpan = layoutManager.isFullSpan(parent, absoluteAdapterPosition)
}
} else {
throw IllegalArgumentException("holder must be RecyclerViewHolderWrapper")
}
}
override fun getItemFactoryByPosition(position: Int): ItemFactory<Any> {
val data = getItemData(position) ?: Placeholder
return itemFactoryStorage.getItemFactoryByData(data) as ItemFactory<Any>
}
override fun getItemFactoryByData(data: DATA): ItemFactory<Any> {
return itemFactoryStorage.getItemFactoryByData(data ?: Placeholder) as ItemFactory<Any>
}
override fun <T : ItemFactory<out Any>> getItemFactoryByClass(itemFactoryClass: Class<T>): T {
return itemFactoryStorage.getItemFactoryByClass(itemFactoryClass)
}
protected open fun onDataListChanged(oldList: List<DATA>, newList: List<DATA>) {
notifyDataSetChanged()
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
parent = recyclerView
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
parent = null
super.onDetachedFromRecyclerView(recyclerView)
}
}