package com.example.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.zhaoyang.driver.R
|
import com.zhaoyang.driver.base.BaseRvAdapter
|
import com.zhaoyang.driver.base.BaseViewHolder
|
import com.zhaoyang.driver.base.MyBaseFragment
|
import com.zhaoyang.driver.netUtls.*
|
import com.zhaoyang.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
|
val gson by lazy {
|
Gson()
|
}
|
|
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
|
}
|
}
|