package cn.flightfeather.thirdappmodule.view.recyclerview
|
|
import android.support.v7.widget.RecyclerView
|
import android.view.View
|
import android.view.ViewGroup
|
import com.chad.library.adapter.base.BaseQuickAdapter
|
import com.chad.library.adapter.base.BaseViewHolder
|
import com.chad.library.adapter.base.loadmore.LoadMoreView
|
|
/**
|
* @author riku
|
* Date: 2019/5/17
|
* recyclerView设置接口
|
*/
|
interface RecyclerViewSetInterface<T> {
|
|
var adapter: BaseRecyclerAdapter<T>?
|
|
fun getRecyclerView(): RecyclerView?
|
|
fun initList() {
|
val layoutIdMap = if (getItemLayoutId() == null) {
|
getItemLayoutIdMap()
|
} else {
|
mapOf(Pair(MySection.DEFAULT_ITEM_TYPE, getItemLayoutId()!!))
|
}
|
adapter = object :
|
BaseRecyclerAdapter<T>(layoutIdMap, getSectionLayoutId(), getSectionDataList()) {
|
override fun convertHead(holder: BaseCustomViewHolder, item: MySection<T>?) {
|
onBindSectionView(holder, item)
|
}
|
|
override fun convert(helper: BaseCustomViewHolder, item: MySection<T>?) {
|
onBindView(helper, item)
|
}
|
|
override fun convertPartView(holder: BaseCustomViewHolder, item: MySection<T>?, payloads: MutableList<Any>) {
|
onBindPartView(holder, item, payloads)
|
}
|
}.apply {
|
getRecyclerView()?.let { bindToRecyclerView(it) }
|
//添加header
|
getHeadViews().iterator().forEach {
|
addHeaderView(it)
|
}
|
//添加footer
|
getFootViews(getRecyclerView()?.parent as ViewGroup).iterator().forEach {
|
addFooterView(it, 0)
|
}
|
//添加item点击事件
|
onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
|
onItemClick(adapter, view, position, getAllDataList())
|
}
|
onItemLongClickListener = BaseQuickAdapter.OnItemLongClickListener { adapter, view, position ->
|
onItemLongClick(adapter, view, position, getAllDataList())
|
}
|
//添加childItem点击事件
|
onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { adapter, view, position ->
|
onItemChildClick(adapter, view, position, getAllDataList())
|
}
|
onItemChildLongClickListener = BaseQuickAdapter.OnItemChildLongClickListener { adapter, view, position ->
|
onItemChildLongClick(adapter, view, position, getAllDataList())
|
}
|
//添加动画
|
openLoadAnimation(getLoadAnimationByDefault())
|
//设置空布局
|
getMyLoadingView()?.let { emptyView = it }
|
|
getLoadMoreView()?.let { setLoadMoreView(it)}
|
}
|
//加载更多
|
setLoadMore()
|
//下拉获取
|
setUpFetch()
|
|
getRecyclerView()?.apply {
|
layoutManager = getMyLayoutManager()
|
getDivider()?.let { addItemDecoration(it) }
|
}?.adapter = adapter
|
}
|
|
fun setLoadMore() {
|
if (enableLoadMore()) {
|
adapter?.apply {
|
setOnLoadMoreListener((BaseQuickAdapter.RequestLoadMoreListener {
|
onLoadMoreStart()
|
onLoadMoreRequested()
|
}), getRecyclerView())
|
}
|
}
|
}
|
|
fun setUpFetch() {
|
if (enableUpFetch()) {
|
adapter?.apply {
|
isUpFetchEnable = true
|
setStartUpFetchPosition(0)
|
setUpFetchListener {
|
isUpFetching = true
|
onUpFetchRequested(this, getRecyclerView())
|
if (!isLoadMoreEnable) {
|
setLoadMore()
|
}
|
}
|
}
|
}
|
}
|
|
fun getAllDataList(): List<T> {
|
val list = adapter?.data as List<MySection<T>>
|
val datas = mutableListOf<T>()
|
list.forEach {
|
datas.add(it.t)
|
}
|
return datas
|
}
|
|
/**
|
* 将列表数据替换成新的数据
|
*/
|
fun setNewData(dataList: List<T>) {
|
val resultList = convertToSection(dataList)
|
addSection(resultList)
|
adapter?.setNewData(resultList)
|
onDataUpdated(adapter)
|
}
|
|
/**
|
* 在原数据基础上添加新数据
|
*/
|
fun addData(dataList: List<T>) {
|
val resultList = convertToSection(dataList)
|
addSection(resultList)
|
adapter?.addData(resultList)
|
onDataUpdated(adapter)
|
}
|
|
fun addData(pos: Int, dataList: List<T>){
|
val resultList = convertToSection(dataList)
|
addSection(resultList)
|
adapter?.addData(pos, resultList)
|
onDataUpdated(adapter, pos)
|
}
|
|
/**
|
* 按照数据状态刷新界面
|
*/
|
fun refreshLoadingStatus(state: LoadState?) {
|
when (state) {
|
LoadState.RefreshDone -> {
|
onRefreshDone()
|
getMyEmptyView()?.let { adapter?.emptyView = it }
|
}
|
LoadState.LoadMoreEnd -> {
|
onLoadMoreEnd()
|
adapter?.loadMoreEnd(getLoadMoreEndGone())
|
getMyEmptyView()?.let { adapter?.emptyView = it }
|
}
|
LoadState.LoadMoreComplete -> {
|
onLoadMoreComplete()
|
adapter?.loadMoreComplete()
|
getMyEmptyView()?.let { adapter?.emptyView = it }
|
}
|
LoadState.NONE -> {
|
onRefreshNone()
|
adapter?.loadMoreEnd(getLoadMoreEndGone())
|
getMyLoadingView()?.let { adapter?.emptyView = it }
|
}
|
LoadState.RefreshFail -> {
|
onRefreshFail()
|
getMyLoadFailView()?.let { adapter?.emptyView = it }
|
}
|
LoadState.LoadMoreFail -> {
|
onLoadMoreFail()
|
adapter?.loadMoreFail()
|
getMyLoadFailView()?.let { adapter?.emptyView = it }
|
}
|
else -> onRefreshNone()
|
}
|
}
|
|
fun onRefreshDone()
|
fun onRefreshFail()
|
fun onLoadMoreEnd()
|
fun onLoadMoreComplete()
|
fun onLoadMoreFail()
|
fun onRefreshNone()
|
|
fun getMyLayoutManager(): RecyclerView.LayoutManager
|
|
//获取列表的itemView的ID
|
fun getItemLayoutId(): Int?
|
|
//获取列表的itemView的ID, 根据自定义的itemType分类,实现同时展示多种item样式
|
fun getItemLayoutIdMap(): Map<Int, Int>
|
|
//获取分组头部view的ID
|
fun getSectionLayoutId(): Int
|
|
//自定义是否需要添加列表分组
|
fun addSection(dataList: MutableList<MySection<T>>) = Unit
|
|
//将数据转换为列表能接受的固定结构
|
fun convertToSection(list: List<T>): MutableList<MySection<T>> = MySection.convert2Section(list)
|
|
//数据更新结束
|
fun onDataUpdated(adapter: BaseRecyclerAdapter<T>?, pos: Int? = null) = Unit
|
|
//获取适配器数据结构列表
|
fun getSectionDataList(): List<MySection<T>>
|
|
//获取header的view绑定方法
|
fun onBindSectionView(holder: BaseCustomViewHolder?, item: MySection<T>?) = Unit
|
|
//获取列表item中的view绑定方法
|
fun onBindView(holder: BaseCustomViewHolder, item: MySection<T>?)
|
|
fun onBindPartView(holder: BaseCustomViewHolder, item: MySection<T>?, payloads: MutableList<Any>)= Unit
|
|
//获取headView
|
fun getHeadViews(): Array<View> = emptyArray()
|
|
//获取footView
|
fun getFootViews(viewGroup: ViewGroup): Array<View> = emptyArray()
|
|
//item点击事件
|
fun onItemClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>)
|
|
//item长点击事件
|
fun onItemLongClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>) =
|
true
|
|
//childItem点击事件
|
fun onItemChildClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>)
|
|
//childItem长点击事件
|
fun onItemChildLongClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<T>):Boolean = true
|
|
//入场动画
|
fun getLoadAnimationByDefault(): Int = BaseQuickAdapter.ALPHAIN
|
|
//<editor-fold desc="加载更多">
|
fun enableLoadMore(): Boolean
|
|
fun onLoadMoreStart()
|
|
fun onLoadMoreRequested()
|
//</editor-fold>
|
|
//<editor-fold desc="下拉获取">
|
fun enableUpFetch(): Boolean
|
|
fun onUpFetchRequested(adapter: BaseRecyclerAdapter<T>, recyclerView: RecyclerView?) = Unit
|
//</editor-fold>
|
|
fun getMyLoadingView(): View? = null
|
|
//获取空布局
|
fun getMyEmptyView(): View?
|
|
//获取空布局
|
fun getMyLoadFailView(): View? = null
|
|
//获取加载布局
|
fun getLoadMoreView(): LoadMoreView? = null
|
|
//分割线
|
fun getDivider(): RecyclerView.ItemDecoration? = null
|
|
//加载完成后, 加载界面是否消失
|
fun getLoadMoreEndGone(): Boolean = false
|
|
}
|
|
/**
|
* 扩展remove 方法, 可以根据传入的编号列表逐个完成删除动画
|
*/
|
fun <T> BaseRecyclerAdapter<T>.remove(posList: List<Int>) {
|
val tempPosList = posList.sortedBy { it }
|
for (i in tempPosList.indices) {
|
this.remove((tempPosList[i] - i))
|
}
|
}
|