lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
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
package cn.sinata.xldutils.activity
 
import android.os.Bundle
import androidx.core.content.ContextCompat
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.sinata.xldutils.R
import cn.sinata.xldutils.adapter.HFRecyclerAdapter
import cn.sinata.xldutils.sysErr
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import org.jetbrains.anko.find
import org.jetbrains.anko.textColor
import org.jetbrains.anko.wrapContent
import kotlin.properties.Delegates
 
/**
 * recyclerView 的activity 。
 */
abstract class RecyclerActivity : TitleActivity() {
 
    val mSwipeRefreshLayout by lazy {
        find<SwipeRefreshRecyclerLayout>(R.id.swipeRefreshLayout)
    }
    protected open var rootFl by Delegates.notNull<LinearLayout>()
    private var emptyView: TextView? = null
    var adapter : RecyclerView.Adapter<*>? = null
    var page = 1
 
    /**
     * 适配器
     */
    protected abstract fun adapter():RecyclerView.Adapter<*>
    /**
     * layoutManager 默认LinearLayoutManager
     */
    protected open fun layoutManager():RecyclerView.LayoutManager = LinearLayoutManager(this)
 
    /**
     * 模式。默认上下拉均支持
     */
    protected open fun mode():SwipeRefreshRecyclerLayout.Mode = SwipeRefreshRecyclerLayout.Mode.Both
    /**
     * 下拉刷新
     */
    protected open fun pullDownRefresh(){}
 
    /**
     * 上拉更多。
     */
    protected open fun loadMore(){}
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.base_recyclerview_layout)
        rootFl = find(R.id.root_layout)
        mSwipeRefreshLayout.setLayoutManager(layoutManager())
        mSwipeRefreshLayout.setMode(mode())
        mSwipeRefreshLayout.isRefreshing = false
        adapter = adapter()
        mSwipeRefreshLayout.setAdapter(adapter)
        mSwipeRefreshLayout.setOnRefreshListener(object :SwipeRefreshRecyclerLayout.OnRefreshListener{
            override fun onRefresh() {
                pullDownRefresh()
            }
 
            override fun onLoadMore() {
                sysErr("-----------onLoadMore--->")
                loadMore()
            }
        })
 
    }
 
    /**
     * 水平分割线,默认1px,颜色为dividing_line_color
     */
    protected fun addItemDecoration(colorId:Int = R.color.dividing_line_color, size:Int=1){
        val item = HorizontalDividerItemDecoration.Builder(this)
                .size(size)
                .color(ContextCompat.getColor(this,colorId))
                .build()
        addItemDecoration(item)
    }
 
    protected fun addItemDecoration(itemDecoration: RecyclerView.ItemDecoration){
        mSwipeRefreshLayout.addItemDecoration(itemDecoration)
    }
 
    /**
     * 可要可不要。可以直接在需要的地方用里面那句。
     */
    fun setRefresh(refresh: Boolean) {
        mSwipeRefreshLayout.isRefreshing = refresh
    }
 
    private val observer:RecyclerView.AdapterDataObserver = object :RecyclerView.AdapterDataObserver(){
        override fun onChanged() {
            super.onChanged()
            toggleEmptyView()
        }
    }
 
    private fun toggleEmptyView() {
        var count = adapter!!.itemCount
        if (adapter is HFRecyclerAdapter<*>) {
            count = (adapter as HFRecyclerAdapter<*>).getDataItemCount()
        }
        if (emptyView != null) {
            if (count > 0) {
                emptyView!!.visibility = View.GONE
            } else {
                emptyView!!.visibility = View.VISIBLE
            }
        }
    }
 
    protected fun addEmptyView(text:String,leftRes:Int=0,topRes:Int=0,rightRes:Int=0,bottomRes:Int=0) {
        if (emptyView == null) {
            emptyView = TextView(this)
            val lp = FrameLayout.LayoutParams(wrapContent, wrapContent)
            lp.gravity = Gravity.CENTER
            emptyView!!.layoutParams = lp
            emptyView!!.gravity = Gravity.CENTER
            emptyView!!.textColor = ContextCompat.getColor(this,R.color.textColor99)
            emptyView!!.setTextSize(TypedValue.COMPLEX_UNIT_SP,16f)
            emptyView!!.setCompoundDrawablesWithIntrinsicBounds(leftRes,topRes,rightRes,bottomRes)
            rootFl.addView(emptyView,0)
            if (adapter != null) {
                adapter!!.registerAdapterDataObserver(observer)
                toggleEmptyView()
            }
        }
        emptyView!!.text = text
    }
 
    override fun onDestroy() {
        try {
            adapter!!.unregisterAdapterDataObserver(observer)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        //同步置空view
        emptyView = null
        super.onDestroy()
    }
 
}