package com.future.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.future.driver.R
|
import com.future.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()
|
|
}
|
|
}
|