lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
284
package com.driver.emanagercar.ui.base
 
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.sinata.xldutils.activitys.BaseActivity
import com.kuanzhai.driver.R
import com.kuanzhai.driver.base.BaseRvAdapter
import com.kuanzhai.driver.base.BaseViewHolder
import com.kuanzhai.driver.base.MyBaseFragment
import com.kuanzhai.driver.netUtls.*
import com.kuanzhai.driver.utils.StatesBarUtil
import com.google.gson.Gson
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.fragment_base_recycler.*
import java.util.*
 
/**
 * Created by ts_xiaoA on 2019/9/25 09:59
 * E-Mail Address:443502578@qq.com
 * Desc: 列表fragment
 */
abstract class BaseRecyclerFragment<Item>(var adapter: BaseRvAdapter<Item>) : MyBaseFragment() {
    protected var page = 1
    protected var pageSize = 10
    protected var isShowNothingLayout = true
    protected var nothingMessage: String = ""
    protected var alwaysShowNoMoreData = false
    private var layoutManager: RecyclerView.LayoutManager? = null
 
 
    override fun layoutId(): Int {
        return R.layout.fragment_base_recycler
    }
 
 
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        nothingMessage = "暂无相关数据"
        if (topViewResId != 0) {
            val topBinding = LayoutInflater.from(activity).inflate(topViewResId, fl_top, true)
            onBindTopView(topBinding)
        }
        if (bottomViewResId != 0) {
            val bottomBinding = LayoutInflater.from(activity)
                .inflate(bottomViewResId, fl_bottom, true)
            onBindBottomView(bottomBinding)
        }
        pageSize = setPageSize(10)
        adapter.context = mContext
        layoutManager = getLayoutManager()
        recycler_view?.layoutManager = layoutManager
        addItemDecoration(recycler_view)
        recycler_view?.adapter = adapter
        adapter.setOnItemClickListener { view: View, position: Int ->
            onItemClick(
                view,
                position
            )
        }
        adapter.setHeadView(headLayoutId)
        adapter.setOnHeaderViewCreator { view: View? ->
            onInitHeaderView(
                view
            )
        }
        adapter.setOnHeadViewBinder { holder: BaseViewHolder?, view: View? ->
            onBindHeadView(
                holder,
                view
            )
        }
        smart_refresh_layout!!.setOnRefreshListener { refreshLayout: RefreshLayout? -> refresh() }
        smart_refresh_layout!!.setOnLoadMoreListener { refreshLayout: RefreshLayout? -> loadMore() }
        super.onActivityCreated(savedInstanceState)
        init()
    }
 
    open fun setPageSize(i: Int): Int {
        return i
    }
 
 
    fun setFullScreenMode(isLight: Boolean) {
        if (isLight) {
            StatesBarUtil.fullScreenLightMode(activity)
        } else {
            StatesBarUtil.fullScreenDarkMode(activity)
        }
    }
 
    /***
     * 添加一个跟着滑动的顶部View
     */
    open fun addTopView(view: View?) {
        fl_h_top.addView(view)
    }
 
 
    fun setNothingMessages(str: String) {
        this.nothingMessage = str;
    }
 
 
    protected fun onBackClick(view: View?) {
        if (activity != null) activity?.onBackPressed()
    }
 
    fun setShowNothingLayouts(isShowNothingLayout: Boolean) {
        this.isShowNothingLayout = isShowNothingLayout
    }
 
    open fun init() {
        if (pathUrl() != null) {
            smart_refresh_layout?.autoRefresh()
        }
        onInit()
    }
 
    open fun autoRefresh() {
        page = 1
        smart_refresh_layout?.autoRefresh()
    }
 
    open fun onInit() {
 
    }
 
    //获取布局管理器,默认LinearLayoutManager
    open fun getLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(getContext())
    }
 
 
    protected abstract fun pathUrl(): String? //网络请求的路径
    protected abstract fun mapObject(): HashMap<String?, Any?>? //网络请求的map
    protected abstract fun mapString(): HashMap<String?, String?>? //网络请求的map
    protected abstract fun getDatas(t: String?): List<Item> //解析数据
    //为recyclerView添加装饰、分割线
    fun addItemDecoration(recyclerView: RecyclerView?) {}
 
    //获取界面上方的布局(不随recyclerView滑动)
    protected val topViewResId: Int = 0
 
    //获取界面下方的布局(不随recyclerView滑动)
    open val bottomViewResId: Int = 0
 
    //绑定上方布局数据
    open fun onBindTopView(view: View?) {}
 
    //绑定底部布局数据
    open fun onBindBottomView(view: View?) {}
 
    //获取recyclerView的header布局
    open val headLayoutId: Int = 0
 
    //headView
    open fun onInitHeaderView(view: View?) {}
 
    /***
     * 禁用或者开启上下拉
     */
    open fun setPull(b: Boolean) {
        smart_refresh_layout?.setEnableLoadMore(b)
        smart_refresh_layout?.setEnableRefresh(b)
    }
 
    //绑定header布局数据
    protected fun onBindHeadView(
        holder: BaseViewHolder?,
        view: View?
    ) {
    }
 
    //item点击事件
    abstract fun onItemClick(view: View?, position: Int)
 
    //刷新
    open fun refresh() {
        if (pathUrl() == null) {
            ll_nothing?.visibility = if (isShowNothingLayout) View.VISIBLE else View.GONE
            smart_refresh_layout?.finishRefresh()
            return
        }
        page = 1
        callRefresh()
    }
 
 
 
    fun callRefresh() {
        val map = mapObject()
        map?.set("size",10)
        map?.set("pageNum",page)
        callNet(pathUrl()!!,map!!,{
            if (it == null || it.isEmpty()) {
                return@callNet
            }
            val data = getDatas(it)
            adapter.data.clear()
            adapter.data.addAll(data)
            tv_nothing?.text = nothingMessage
            ll_nothing?.visibility =
                if (adapter.data.size == 0) View.VISIBLE else View.GONE
            if (headLayoutId != 0 && layoutManager != null) {
                val headView = layoutManager!!.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 < pageSize || alwaysShowNoMoreData) {
                smart_refresh_layout?.finishLoadMoreWithNoMoreData()
            }
            adapter.notifyDataSetChanged()
            noShowNothing()
        },{
            noShowNothing()
            dismissDialog()
        })
    }
 
 
    fun callLoadMore() {
        val map = mapObject()
        map?.set("size",pageSize)
        map?.set("pageNum",page)
        callNet(pathUrl()!!,map!!,{
            if (it == null || it.isEmpty()) {
                dismissDialog()
                return@callNet
            }
            val data = getDatas(it)
            loadOver()
            adapter.getData().addAll(data)
            ll_nothing!!.visibility =
                if (adapter.data.size == 0) View.VISIBLE else View.GONE
            if (data.size < pageSize) {
                smart_refresh_layout!!.finishLoadMoreWithNoMoreData()
            }
            adapter.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()
 
    }
 
    fun setAlwayShowNoMoreData(alwaysShowNoMoreData: Boolean) {
        this.alwaysShowNoMoreData = alwaysShowNoMoreData
    }
 
    //加载更多
    protected fun loadMore() {
        if (pathUrl() == null) {
            ll_nothing?.visibility = if (isShowNothingLayout) View.VISIBLE else View.GONE
            smart_refresh_layout!!.finishLoadMore()
            return
        }
        page++
        callLoadMore()
    }
 
    companion object {
        protected const val REQUEST_NORMAL = BaseActivity.REQUEST_NORMAL
    }
}