| | |
| | | //package cn.flightfeather.thirdapp.view.recyclerview |
| | | // |
| | | //import android.annotation.SuppressLint |
| | | //import android.arch.lifecycle.LifecycleOwner |
| | | //import android.arch.lifecycle.Observer |
| | | //import android.content.Context |
| | | //import android.support.v4.content.ContextCompat |
| | | //import android.support.v4.widget.SwipeRefreshLayout |
| | | //import android.support.v7.widget.LinearLayoutManager |
| | | //import android.support.v7.widget.RecyclerView |
| | | //import android.view.LayoutInflater |
| | | //import android.view.View |
| | | //import com.chad.library.adapter.base.BaseQuickAdapter |
| | | //import com.chad.library.adapter.base.BaseViewHolder |
| | | // |
| | | ///** |
| | | // * RecyclerView模块 |
| | | // * @author riku |
| | | // * Date: 2019/11/22 |
| | | // * reference layout is [R.layout.layout_recycler_view_refresh] |
| | | // */ |
| | | //abstract class RecyclerViewPanel<T>( |
| | | // private val viewModel: BaseViewModel<T>, |
| | | // private val lifecycleObserver: LifecycleOwner, |
| | | // private val rootView: View? = null |
| | | //) : RecyclerViewSetInterface<T>, SwipeRefreshLayoutSetInterface { |
| | | // |
| | | // constructor( |
| | | // viewModel: BaseViewModel<T>, |
| | | // lifecycleObserver: LifecycleOwner, |
| | | // recyclerView: RecyclerView, |
| | | // context: Context?, |
| | | // refreshLayout: SwipeRefreshLayout? = null |
| | | // ) : this(viewModel, lifecycleObserver) { |
| | | // context?.let { this.context = it } |
| | | // tempRecyclerView = recyclerView |
| | | // this.tempRefreshLayout = refreshLayout |
| | | // } |
| | | // |
| | | // private lateinit var context: Context |
| | | // private var tempRecyclerView: RecyclerView? = null |
| | | // private var tempRefreshLayout: SwipeRefreshLayout? = null |
| | | // |
| | | // override var adapter: BaseRecyclerAdapter<T>? = null |
| | | // |
| | | // private var mRecyclerView: RecyclerView? = null |
| | | // |
| | | // private var refreshLayout: SwipeRefreshLayout? = null |
| | | // |
| | | // init { |
| | | // if (rootView != null) { |
| | | // this@RecyclerViewPanel.context = rootView.context |
| | | // } |
| | | // } |
| | | // |
| | | // open fun init() { |
| | | // initViews() |
| | | // initList() |
| | | // initSwipeRefreshLayout() |
| | | // initObserver() |
| | | // } |
| | | // |
| | | // private fun initViews() { |
| | | // mRecyclerView = rootView?.findViewById(R.id.recycler_view) ?: tempRecyclerView |
| | | // refreshLayout = rootView?.findViewById(R.id.refresh_layout) ?: tempRefreshLayout |
| | | // } |
| | | // |
| | | // open fun initObserver() { |
| | | // viewModel.loading.observe(lifecycleObserver, Observer { |
| | | // refreshLoadingStatus(it) |
| | | // }) |
| | | // |
| | | // viewModel.dataList.observe(lifecycleObserver, Observer { dataList -> |
| | | // dataList?.let { |
| | | // if (viewModel.isFirstData()) { |
| | | // setNewData(it) |
| | | // } else { |
| | | // addData(it) |
| | | // } |
| | | // } |
| | | // }) |
| | | // |
| | | // viewModel.dataChanged.observe(lifecycleObserver, Observer { |
| | | // if (it == true) { |
| | | // adapter?.notifyDataSetChanged() |
| | | // } |
| | | // }) |
| | | // } |
| | | // |
| | | // override fun getRecyclerView(): RecyclerView? = |
| | | // mRecyclerView ?: rootView?.findViewById(R.id.recycler_view) ?: tempRecyclerView |
| | | // |
| | | // override fun onRefreshDone() = stopRefresh() |
| | | // override fun onRefreshFail() = stopRefresh() |
| | | // override fun onLoadMoreEnd() = Unit |
| | | // override fun onLoadMoreComplete() = Unit |
| | | // override fun onLoadMoreFail() = Unit |
| | | // override fun onRefreshNone() = stopRefresh() |
| | | // |
| | | // override fun getMyLayoutManager(): RecyclerView.LayoutManager = |
| | | // LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) |
| | | // |
| | | // override fun getItemLayoutIdMap(): Map<Int, Int> = emptyMap() |
| | | // |
| | | // override fun getSectionLayoutId(): Int = R.layout.recycler_item_section_head |
| | | // |
| | | // override fun getSectionDataList(): List<MySection<T>> = |
| | | // MySection.convert2Section(viewModel.dataList.value) |
| | | // |
| | | // override fun onItemClick( |
| | | // adapter: BaseQuickAdapter<Any?, BaseViewHolder>, |
| | | // view: View, |
| | | // position: Int, |
| | | // dataList: List<T> |
| | | // ) = Unit |
| | | // |
| | | // override fun onItemChildClick( |
| | | // adapter: BaseQuickAdapter<Any?, BaseViewHolder>, |
| | | // view: View, |
| | | // position: Int, |
| | | // dataList: List<T> |
| | | // ) = Unit |
| | | // |
| | | // override fun enableLoadMore(): Boolean = true |
| | | // |
| | | // override fun onLoadMoreStart() { |
| | | // if (getSwipeRefreshLayout()?.isRefreshing == true) { |
| | | // adapter?.loadMoreComplete() |
| | | // return |
| | | // } |
| | | // } |
| | | // |
| | | // override fun onLoadMoreRequested() = viewModel.loadMore() |
| | | // |
| | | // override fun enableUpFetch(): Boolean = false |
| | | // |
| | | // @SuppressLint("InflateParams") |
| | | // override fun getMyEmptyView(): View? = |
| | | // LayoutInflater.from(context).inflate(R.layout.layout_empty_view, null) |
| | | // |
| | | // @SuppressLint("InflateParams") |
| | | // override fun getMyLoadingView(): View? = LayoutInflater.from(context).inflate(R.layout.layout_loading_view, null) |
| | | // |
| | | // @SuppressLint("InflateParams") |
| | | // override fun getMyLoadFailView(): View? = LayoutInflater.from(context).inflate(R.layout.layout_load_fail_view, null) |
| | | // |
| | | // //******************************SwipeRefreshLayout************************************************************ |
| | | // |
| | | // override fun getSwipeRefreshLayout(): SwipeRefreshLayout? = |
| | | // refreshLayout ?: rootView?.findViewById(R.id.refresh_layout) ?: tempRefreshLayout |
| | | // |
| | | // override fun enableRefresh(): Boolean = true |
| | | // |
| | | // override fun needSwipeRefresh(): Boolean = true |
| | | // |
| | | // override fun onRefreshRequested() { |
| | | // //第三方recyclerView的上拉加载结束后,如果下拉刷新执行,则应该重新打开监听 |
| | | // try { |
| | | // if (!adapter?.isLoadMoreEnable!!) { |
| | | // setLoadMore() |
| | | // } |
| | | // } catch (e: Exception) { |
| | | // e.printStackTrace() |
| | | // } |
| | | // viewModel.refresh() |
| | | // } |
| | | // |
| | | // override fun getColorList(): Array<Int> = arrayOf( |
| | | // ContextCompat.getColor(context, R.color.colorPrimary), |
| | | // ContextCompat.getColor(context, R.color.colorPrimaryDark), |
| | | // ContextCompat.getColor(context, R.color.colorAccent) |
| | | // ) |
| | | //} |
| | | package cn.flightfeather.thirdapp.view.recyclerview |
| | | |
| | | import android.annotation.SuppressLint |
| | | import android.arch.lifecycle.LifecycleOwner |
| | | import android.arch.lifecycle.Observer |
| | | import android.content.Context |
| | | import android.support.v4.content.ContextCompat |
| | | import android.support.v4.widget.SwipeRefreshLayout |
| | | import android.support.v7.widget.LinearLayoutManager |
| | | import android.support.v7.widget.RecyclerView |
| | | import android.view.LayoutInflater |
| | | import android.view.View |
| | | import cn.flightfeather.thirdapp.R |
| | | import com.chad.library.adapter.base.BaseQuickAdapter |
| | | import com.chad.library.adapter.base.BaseViewHolder |
| | | |
| | | /** |
| | | * RecyclerView模块 |
| | | * @author riku |
| | | * Date: 2019/11/22 |
| | | * reference layout is [R.layout.layout_recycler_view_refresh] |
| | | */ |
| | | abstract class RecyclerViewPanel<T>( |
| | | private val dataLoadModel: DataLoadModel<T>, |
| | | private val rootView: View? = null |
| | | ) : RecyclerViewSetInterface<T>, SwipeRefreshLayoutSetInterface { |
| | | |
| | | constructor( |
| | | dataLoadModel: DataLoadModel<T>, |
| | | recyclerView: RecyclerView, |
| | | context: Context?, |
| | | refreshLayout: SwipeRefreshLayout? = null |
| | | ) : this(dataLoadModel) { |
| | | context?.let { this.context = it } |
| | | tempRecyclerView = recyclerView |
| | | this.tempRefreshLayout = refreshLayout |
| | | } |
| | | |
| | | private lateinit var context: Context |
| | | private var tempRecyclerView: RecyclerView? = null |
| | | private var tempRefreshLayout: SwipeRefreshLayout? = null |
| | | |
| | | override var adapter: BaseRecyclerAdapter<T>? = null |
| | | |
| | | private var mRecyclerView: RecyclerView? = null |
| | | |
| | | private var refreshLayout: SwipeRefreshLayout? = null |
| | | |
| | | init { |
| | | if (rootView != null) { |
| | | this@RecyclerViewPanel.context = rootView.context |
| | | } |
| | | } |
| | | |
| | | open fun init() { |
| | | initViews() |
| | | initList() |
| | | initSwipeRefreshLayout() |
| | | initObserver() |
| | | } |
| | | |
| | | private fun initViews() { |
| | | mRecyclerView = rootView?.findViewById(R.id.recycler_view) ?: tempRecyclerView |
| | | refreshLayout = rootView?.findViewById(R.id.refresh_layout) ?: tempRefreshLayout |
| | | } |
| | | |
| | | open fun initObserver() { |
| | | dataLoadModel.loadStateListener = { |
| | | refreshLoadingStatus(it) |
| | | } |
| | | |
| | | dataLoadModel.dataLoadListener = { |
| | | if (dataLoadModel.isFirstData()) { |
| | | setNewData(it) |
| | | } else { |
| | | addData(it) |
| | | } |
| | | } |
| | | } |
| | | |
| | | override fun getRecyclerView(): RecyclerView? = |
| | | mRecyclerView ?: rootView?.findViewById(R.id.recycler_view) ?: tempRecyclerView |
| | | |
| | | override fun onRefreshDone() = stopRefresh() |
| | | override fun onRefreshFail() = stopRefresh() |
| | | override fun onLoadMoreEnd() = Unit |
| | | override fun onLoadMoreComplete() = Unit |
| | | override fun onLoadMoreFail() = Unit |
| | | override fun onRefreshNone() = stopRefresh() |
| | | |
| | | override fun getMyLayoutManager(): RecyclerView.LayoutManager = |
| | | LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) |
| | | |
| | | override fun getItemLayoutIdMap(): Map<Int, Int> = emptyMap() |
| | | |
| | | override fun getSectionLayoutId(): Int = R.layout.recycler_item_section_head |
| | | |
| | | override fun getSectionDataList(): List<MySection<T>> = |
| | | MySection.convert2Section(dataLoadModel.dataList) |
| | | |
| | | override fun onItemClick( |
| | | adapter: BaseQuickAdapter<Any?, BaseViewHolder>, |
| | | view: View, |
| | | position: Int, |
| | | dataList: List<T> |
| | | ) = Unit |
| | | |
| | | override fun onItemChildClick( |
| | | adapter: BaseQuickAdapter<Any?, BaseViewHolder>, |
| | | view: View, |
| | | position: Int, |
| | | dataList: List<T> |
| | | ) = Unit |
| | | |
| | | override fun enableLoadMore(): Boolean = true |
| | | |
| | | override fun onLoadMoreStart() { |
| | | if (getSwipeRefreshLayout()?.isRefreshing == true) { |
| | | adapter?.loadMoreComplete() |
| | | return |
| | | } |
| | | } |
| | | |
| | | override fun onLoadMoreRequested() = dataLoadModel.loadMore() |
| | | |
| | | override fun enableUpFetch(): Boolean = false |
| | | |
| | | @SuppressLint("InflateParams") |
| | | override fun getMyEmptyView(): View? = |
| | | LayoutInflater.from(context).inflate(R.layout.layout_empty_view, null) |
| | | |
| | | @SuppressLint("InflateParams") |
| | | override fun getMyLoadingView(): View? = LayoutInflater.from(context).inflate(R.layout.layout_loading_view, null) |
| | | |
| | | @SuppressLint("InflateParams") |
| | | override fun getMyLoadFailView(): View? = LayoutInflater.from(context).inflate(R.layout.layout_load_fail_view, null) |
| | | |
| | | //******************************SwipeRefreshLayout************************************************************ |
| | | |
| | | override fun getSwipeRefreshLayout(): SwipeRefreshLayout? = |
| | | refreshLayout ?: rootView?.findViewById(R.id.refresh_layout) ?: tempRefreshLayout |
| | | |
| | | override fun enableRefresh(): Boolean = true |
| | | |
| | | override fun needSwipeRefresh(): Boolean = true |
| | | |
| | | override fun onRefreshRequested() { |
| | | //第三方recyclerView的上拉加载结束后,如果下拉刷新执行,则应该重新打开监听 |
| | | try { |
| | | if (!adapter?.isLoadMoreEnable!!) { |
| | | setLoadMore() |
| | | } |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | dataLoadModel.refresh() |
| | | } |
| | | |
| | | override fun getColorList(): Array<Int> = arrayOf( |
| | | ContextCompat.getColor(context, R.color.colorPrimary), |
| | | ContextCompat.getColor(context, R.color.colorPrimaryDark), |
| | | ContextCompat.getColor(context, R.color.colorAccent) |
| | | ) |
| | | } |