lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
package com.zhaoyang.driver.base
 
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.zhaoyang.driver.R
import com.zhaoyang.driver.netUtls.callNet
import com.xuexiang.xui.utils.DensityUtils.dp2px
import kotlinx.android.synthetic.main.activity_base_recycler.*
import java.util.HashMap
 
abstract class BaseRecyclerActivity<item>: MyBaseActivity() {
 
    protected abstract fun getDatas(t: String?): List<item> //解析数据
 
    abstract fun pathUrl(): String?
 
    abstract fun mapObject(): HashMap<String?, Any?>?
 
    abstract fun getAdapter(): BaseRvAdapter<item>?
 
    abstract fun getlayoutManager(): RecyclerView.LayoutManager?
 
    var adapters:BaseRvAdapter<item>? = null
 
    protected var nothingMessage: String = ""
 
    protected var alwaysShowNoMoreData = false
 
    protected var isShowNothingLayout = true
 
 
    override fun setContentView() {
        setContentView(R.layout.activity_base_recycler)
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        nothingMessage = "暂无相关数据"
        recycler_view.layoutManager = getlayoutManager()
        adapters = getAdapter()
        recycler_view.adapter = adapters
        smart_refresh_layout.setOnRefreshListener {
            refreshThis()
        }
        smart_refresh_layout.setOnLoadMoreListener {
            loadMore()
        }
 
        adapters!!.setOnItemClickListener { view, position ->
            setOnItemClickListener(view,position)
        }
        refreshThis()
    }
 
    abstract fun setOnItemClickListener(view: View?, position: Int)
 
 
    private fun refreshThis() {
        callRefresh()
    }
 
    open fun addTopView(view: View){
        ll_top_view.addView(view,0)
    }
 
    /***
     * 添加一个view在最底部 并且设置recyclerview的padding值
     */
    fun addMainBottomViewSimple(view:View){
        rl_main_top.addView(view)
        var param = view.layoutParams as RelativeLayout.LayoutParams
        param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        param.bottomMargin = dp2px(14f)
        param.marginStart = dp2px(14f)
        param.marginEnd = dp2px(14f)
        view.post {
            recycler_view.setPadding(0,0,0,view.height+dp2px(30f))
        }
    }
 
 
 
    var page = 1
 
    fun callRefresh() {
        page = 1
        val map = mapObject()
        map?.set("size", PAGE_SIZE)
        map?.set("pageNum",page)
        callNet(pathUrl()!!,map!!,{
            if (it == null || it.isEmpty()) {
                return@callNet
            }
            val data = getDatas(it)
            adapters!!.data.clear()
            adapters!!.data.addAll(data)
            tv_nothing?.text = nothingMessage
            ll_nothing?.visibility =
                if (adapters!!.data.size == 0) View.VISIBLE else View.GONE
            if (getlayoutManager() != null) {
                val headView = getlayoutManager()!!.findViewByPosition(0)
                if (headView != null && headView.visibility == View.VISIBLE) {
                    val layoutParams =
                        ll_nothing?.layoutParams as FrameLayout.LayoutParams
                    layoutParams.topMargin = headView.measuredHeight
                    ll_nothing?.layoutParams = layoutParams
                }
            }
            if (data.size < PAGE_SIZE || alwaysShowNoMoreData) {
                smart_refresh_layout?.finishLoadMoreWithNoMoreData()
            }
            adapters!!.notifyDataSetChanged()
            noShowNothing()
        },{
            noShowNothing()
            dismissDialog()
        })
    }
 
 
    //加载更多
    protected fun loadMore() {
        if (pathUrl() == null) {
            ll_nothing?.visibility = if (isShowNothingLayout) View.VISIBLE else View.GONE
            smart_refresh_layout!!.finishLoadMore()
            return
        }
        page++
        callLoadMore()
    }
 
    fun callLoadMore() {
        val map = mapObject()
        map?.set("size", PAGE_SIZE)
        map?.set("pageNum",page)
        callNet(pathUrl()!!,map!!,{
            if (it == null || it.isEmpty()) {
                dismissDialog()
                return@callNet
            }
            val data = getDatas(it)
            loadOver()
            adapters!!.getData().addAll(data)
            ll_nothing!!.visibility =
                if (adapters!!.data.size == 0) View.VISIBLE else View.GONE
            if (data.size < PAGE_SIZE) {
                smart_refresh_layout!!.finishLoadMoreWithNoMoreData()
            }
            adapters!!.notifyDataSetChanged()
            noShowNothing()
        }) {
            dismissDialog()
            noShowNothing()
        }
    }
 
    open fun loadOver() {
 
    }
 
    open fun noShowNothing() {
        if (!isShowNothingLayout) {
            ll_nothing?.visibility = View.GONE
        }
        smart_refresh_layout?.finishRefresh()
        smart_refresh_layout?.finishLoadMore()
 
    }
 
}