-
Notifications
You must be signed in to change notification settings - Fork 19
/
RefreshRecyclerView.kt
64 lines (56 loc) · 1.94 KB
/
RefreshRecyclerView.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
package com.catchpig.mvvm.widget.refresh
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.catchpig.mvvm.R
import com.catchpig.mvvm.base.adapter.RecyclerAdapter
import com.catchpig.utils.ext.logi
import com.scwang.smart.refresh.layout.constant.RefreshState
class RefreshRecyclerView(
context: Context,
attrs: AttributeSet
) : RefreshLayoutWrapper(context, attrs) {
companion object {
private const val TAG = "RefreshRecyclerView"
}
private var recyclerBackground: Int
private val recyclerView: RecyclerView
init {
val typedArray =
context.obtainStyledAttributes(attrs, R.styleable.RefreshRecyclerView)
recyclerBackground = typedArray.getColor(
R.styleable.RefreshRecyclerView_recycler_background,
Color.TRANSPARENT
)
typedArray.recycle()
recyclerView = RecyclerView(context, attrs)
recyclerView.layoutParams =
LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
addView(recyclerView)
recyclerView.setBackgroundColor(recyclerBackground)
}
fun setLayoutManager(layoutManage: RecyclerView.LayoutManager) {
recyclerView.layoutManager = layoutManage
}
fun <M> setAdapter(adapter: RecyclerAdapter<M, out ViewBinding>) {
recyclerView.adapter = adapter
}
fun <M> updateData(data: MutableList<M>?) {
val adapter = recyclerView.adapter as RecyclerAdapter<M, out ViewBinding>
when (state) {
RefreshState.Refreshing -> {
adapter.set(data)
}
RefreshState.Loading -> {
adapter.add(data)
}
else -> {
"state:$state".logi(TAG)
}
}
updateSuccess(data)
}
}