riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package cn.flightfeather.thirdappmodule.view.recyclerview
 
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.loadmore.LoadMoreView
 
/**
 * @author riku
 * Date: 2019/5/17
 * recyclerView设置接口
 */
interface RecyclerViewSetInterface<T> {
 
    var adapter: BaseRecyclerAdapter<T>?
 
    fun getRecyclerView(): RecyclerView?
 
    fun initList() {
        val layoutIdMap = if (getItemLayoutId() == null) {
            getItemLayoutIdMap()
        } else {
            mapOf(Pair(MySection.DEFAULT_ITEM_TYPE, getItemLayoutId()!!))
        }
        adapter = object :
            BaseRecyclerAdapter<T>(layoutIdMap, getSectionLayoutId(), getSectionDataList()) {
            override fun convertHead(holder: BaseCustomViewHolder, item: MySection<T>?) {
                onBindSectionView(holder, item)
            }
 
            override fun convert(helper: BaseCustomViewHolder, item: MySection<T>?) {
                onBindView(helper, item)
            }
 
            override fun convertPartView(holder: BaseCustomViewHolder, item: MySection<T>?, payloads: MutableList<Any>) {
                onBindPartView(holder, item, payloads)
            }
        }.apply {
            getRecyclerView()?.let { bindToRecyclerView(it) }
            //添加header
            getHeadViews().iterator().forEach {
                addHeaderView(it)
            }
            //添加footer
            getFootViews(getRecyclerView()?.parent as ViewGroup).iterator().forEach {
                addFooterView(it, 0)
            }
            //添加item点击事件
            onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                onItemClick(adapter, view, position, getAllDataList())
            }
            onItemLongClickListener = BaseQuickAdapter.OnItemLongClickListener { adapter, view, position ->
                onItemLongClick(adapter, view, position, getAllDataList())
            }
            //添加childItem点击事件
            onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { adapter, view, position ->
                onItemChildClick(adapter, view, position, getAllDataList())
            }
            onItemChildLongClickListener = BaseQuickAdapter.OnItemChildLongClickListener { adapter, view, position ->
                onItemChildLongClick(adapter, view, position, getAllDataList())
            }
            //添加动画
            openLoadAnimation(getLoadAnimationByDefault())
            //设置空布局
            getMyLoadingView()?.let { emptyView = it }
 
            getLoadMoreView()?.let { setLoadMoreView(it)}
        }
        //加载更多
        setLoadMore()
        //下拉获取
        setUpFetch()
 
        getRecyclerView()?.apply {
            layoutManager = getMyLayoutManager()
            getDivider()?.let { addItemDecoration(it) }
        }?.adapter = adapter
    }
 
    fun setLoadMore() {
        if (enableLoadMore()) {
            adapter?.apply {
                setOnLoadMoreListener((BaseQuickAdapter.RequestLoadMoreListener {
                    onLoadMoreStart()
                    onLoadMoreRequested()
                }), getRecyclerView())
            }
        }
    }
 
    fun setUpFetch() {
        if (enableUpFetch()) {
            adapter?.apply {
                isUpFetchEnable = true
                setStartUpFetchPosition(0)
                setUpFetchListener {
                    isUpFetching = true
                    onUpFetchRequested(this, getRecyclerView())
                    if (!isLoadMoreEnable) {
                        setLoadMore()
                    }
                }
            }
        }
    }
 
    fun getAllDataList(): List<T> {
        val list = adapter?.data as List<MySection<T>>
        val datas = mutableListOf<T>()
        list.forEach {
            datas.add(it.t)
        }
        return datas
    }
 
    /**
     * 将列表数据替换成新的数据
     */
    fun setNewData(dataList: List<T>) {
        val resultList = convertToSection(dataList)
        addSection(resultList)
        adapter?.setNewData(resultList)
        onDataUpdated(adapter)
    }
 
    /**
     * 在原数据基础上添加新数据
     */
    fun addData(dataList: List<T>) {
        val resultList = convertToSection(dataList)
        addSection(resultList)
        adapter?.addData(resultList)
        onDataUpdated(adapter)
    }
 
    fun addData(pos: Int, dataList: List<T>){
        val resultList = convertToSection(dataList)
        addSection(resultList)
        adapter?.addData(pos, resultList)
        onDataUpdated(adapter, pos)
    }
 
    /**
     * 按照数据状态刷新界面
     */
    fun refreshLoadingStatus(state: LoadState?) {
        when (state) {
            LoadState.RefreshDone -> {
                onRefreshDone()
                getMyEmptyView()?.let { adapter?.emptyView = it }
            }
            LoadState.LoadMoreEnd -> {
                onLoadMoreEnd()
                adapter?.loadMoreEnd(getLoadMoreEndGone())
                getMyEmptyView()?.let { adapter?.emptyView = it }
            }
            LoadState.LoadMoreComplete -> {
                onLoadMoreComplete()
                adapter?.loadMoreComplete()
                getMyEmptyView()?.let { adapter?.emptyView = it }
            }
            LoadState.NONE -> {
                onRefreshNone()
                adapter?.loadMoreEnd(getLoadMoreEndGone())
                getMyLoadingView()?.let { adapter?.emptyView = it }
            }
            LoadState.RefreshFail -> {
                onRefreshFail()
                getMyLoadFailView()?.let { adapter?.emptyView = it }
            }
            LoadState.LoadMoreFail -> {
                onLoadMoreFail()
                adapter?.loadMoreFail()
                getMyLoadFailView()?.let { adapter?.emptyView = it }
            }
            else -> onRefreshNone()
        }
    }
 
    fun onRefreshDone()
    fun onRefreshFail()
    fun onLoadMoreEnd()
    fun onLoadMoreComplete()
    fun onLoadMoreFail()
    fun onRefreshNone()
 
    fun getMyLayoutManager(): RecyclerView.LayoutManager
 
    //获取列表的itemView的ID
    fun getItemLayoutId(): Int?
 
    //获取列表的itemView的ID, 根据自定义的itemType分类,实现同时展示多种item样式
    fun getItemLayoutIdMap(): Map<Int, Int>
 
    //获取分组头部view的ID
    fun getSectionLayoutId(): Int
 
    //自定义是否需要添加列表分组
    fun addSection(dataList: MutableList<MySection<T>>) = Unit
 
    //将数据转换为列表能接受的固定结构
    fun convertToSection(list: List<T>): MutableList<MySection<T>> = MySection.convert2Section(list)
 
    //数据更新结束
    fun onDataUpdated(adapter: BaseRecyclerAdapter<T>?, pos: Int? = null) = Unit
 
    //获取适配器数据结构列表
    fun getSectionDataList(): List<MySection<T>>
 
    //获取header的view绑定方法
    fun onBindSectionView(holder: BaseCustomViewHolder?, item: MySection<T>?) = Unit
 
    //获取列表item中的view绑定方法
    fun onBindView(holder: BaseCustomViewHolder, item: MySection<T>?)
 
    fun onBindPartView(holder: BaseCustomViewHolder, item: MySection<T>?, payloads: MutableList<Any>)= Unit
 
    //获取headView
    fun getHeadViews(): Array<View> = emptyArray()
 
    //获取footView
    fun getFootViews(viewGroup: ViewGroup): Array<View> = emptyArray()
 
    //item点击事件
    fun onItemClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>)
 
    //item长点击事件
    fun onItemLongClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>) =
        true
 
    //childItem点击事件
    fun onItemChildClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>)
 
    //childItem长点击事件
    fun onItemChildLongClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>):Boolean = true
 
    //入场动画
    fun getLoadAnimationByDefault(): Int = BaseQuickAdapter.ALPHAIN
 
    //<editor-fold desc="加载更多">
    fun enableLoadMore(): Boolean
 
    fun onLoadMoreStart()
 
    fun onLoadMoreRequested()
    //</editor-fold>
 
    //<editor-fold desc="下拉获取">
    fun enableUpFetch(): Boolean
 
    fun onUpFetchRequested(adapter: BaseRecyclerAdapter<T>, recyclerView: RecyclerView?) = Unit
    //</editor-fold>
 
    fun getMyLoadingView(): View? = null
 
    //获取空布局
    fun getMyEmptyView(): View?
 
    //获取空布局
    fun getMyLoadFailView(): View? = null
 
    //获取加载布局
    fun getLoadMoreView(): LoadMoreView? = null
 
    //分割线
    fun getDivider(): RecyclerView.ItemDecoration? = null
 
    //加载完成后, 加载界面是否消失
    fun getLoadMoreEndGone(): Boolean = false
 
}
 
/**
 * 扩展remove 方法, 可以根据传入的编号列表逐个完成删除动画
 */
fun <T> BaseRecyclerAdapter<T>.remove(posList: List<Int>) {
    val tempPosList = posList.sortedBy { it }
    for (i in tempPosList.indices) {
        this.remove((tempPosList[i] - i))
    }
}