这里是利用系统的 SwipeRefreshLayout,RecyclerView ,加上开源库 BaseRecyclerViewAdapterHelper 组合而成的一个View ,可控制是否刷新,加载,不用每次都去写一个 SwipeRefreshLayout 嵌套 RecyclerView 。
使用 RecyclerView 和 BaseRecyclerViewAdapterHelper 版本如下:
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'
来看一下实现代码:
private fun initView() {
// 模拟页面初始化数据请求,
onRefresh()
// mList.addAll(getList())
initAdapter()
// 初始化相关配置
activity_swipe_refresh.initOptions(object : SwipeRefreshView.ConfigOptions {
override fun configAdapter(): BaseQuickAdapter<*, BaseViewHolder> {
/**
* 初始化 Adapter ,此处可以在 return 之前对 Adapter 进行一系列的需求配置。 比如 setEmptyView 等等~
*
* getAdapter() 得到的是此处设置的 adapter
*/
return mAdapter
}
override fun layoutManager(): RecyclerView.LayoutManager = LinearLayoutManager(this@SwipeRefreshActivity)
override fun configRecyclerView(recyclerView: RecyclerView) {
/**
* 这里可进行一些你想要的 RecyclerView 设置, 此方法为非必选
*
* 不想实现此方法,也可通过 activity_swipe_refresh.getRecyclerView() 来获取 RecyclerView
*/
super.configRecyclerView(recyclerView)
}
override fun configSwipeRefreshLayout(refreshLayout: SwipeRefreshLayout) {
/**
* 这里可进行一些你想要的 SwipeRefreshLayout 相关设置,比如刷新的颜色,背景色
*
* 此方法为非必选
*
* 不想实现此方法,也可通过 activity_swipe_refresh.getSwipeRefreshLayout() 来获取 SwipeRefreshLayout
*/
super.configSwipeRefreshLayout(refreshLayout)
}
override fun setEnableLoadMore(): Boolean {
/**
* 是否可加载更多 该方法非必须,默认 false
* 等同于 setEnableRefresh(boolean)
*/
return super.setEnableLoadMore()
}
override fun setEnableRefresh(): Boolean {
/**
* 是否可刷新 该方法非必须,默认 false
* 等同于 setEnableRefresh(boolean)
*/
return super.setEnableRefresh()
}
})
// 是否可刷新,default false
activity_swipe_refresh.setEnableRefresh(true)
// 是否可加载更多,default false
activity_swipe_refresh.setEnableLoadMore(true)
// 设置空布局 要在 setAdapter 之后进行设置
// mAdapter.setEmptyView(R.layout.layout_empty)// 可以直接通过当前 Adapter 引用进行设置,
// activity_swipe_refresh.getAdapter().setEmptyView(R.layout.layout_empty) // 也可以通过 getAdapter 进行设置
activity_swipe_refresh.setEmptyView(R.layout.layout_empty)
//设置监听
activity_swipe_refresh.swipeRefreshListener = this
}
private fun initAdapter() {
mAdapter = object : BaseQuickAdapter(android.R.layout.simple_list_item_1, mList) {
override fun convert(helper: BaseViewHolder?, item: String?) {
if (helper != null) {
val textView: TextView = helper.getView(android.R.id.text1)
textView.apply {
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200)// 这里高度值是像素
gravity = Gravity.CENTER_VERTICAL
}
textView.text = item
}
}
}
}
override fun onRefresh() {
// 刷新
pageIndex = 1
Handler().postDelayed({
mAdapter.setNewData(getList())
activity_swipe_refresh.loadMoreComplete()
}, 1000)
}
private var pageIndex = 1
override fun onLoadMoreRequested() {
// 加载更多。
Handler().postDelayed({
pageIndex++
mAdapter.addData(getList())
if (pageIndex <= 2) {
// 当前页加载完成 非最后一页
activity_swipe_refresh.loadMoreComplete()
} else {
// 加载完成,最后一页,参数控制是否显示加载结束布局
activity_swipe_refresh.loadMoreEnd(false)
}
}, 1000)
}
布局文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.lvfq.code.swipe.SwipeRefreshView
android:id="@+id/activity_swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LinearLayout>
这里是模拟数据请求,用于实际项目时,可根据实际情况进行调整。
另外如果需要额外的属性,可通过 SwipeRefreshView 获取 SwipeRefreshLayout , RecyclerView , 和 Adapter 进行设置。
至于 SwipeRefreshView 相关方法介绍,我下面贴出其代码,方法上面都写有注释:
class SwipeRefreshView(context: Context) : FrameLayout(context), BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
constructor(context: Context, attrs: AttributeSet) : this(context)
private var swipeView: SwipeRefreshLayout = SwipeRefreshLayout(context)
private var recyclerView: RecyclerView
private lateinit var mAdapter: BaseQuickAdapter<*, BaseViewHolder>
var swipeRefreshListener: SwipeRefreshListener? = null
// 是否可刷新
private var isCanRefresh = false
// 是否可加载更多
private var isCanLoadMore = false
init {
swipeView.apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
}
recyclerView = RecyclerView(context)
recyclerView.apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
}
recyclerView.isNestedScrollingEnabled = false
// 设置刷新的默认背景颜色
swipeView.setProgressBackgroundColorSchemeResource(R.color.white)
// 设置刷新的进度颜色
swipeView.setColorSchemeResources(R.color.c_00ce9b, R.color.c_11c111, R.color.c_1495eb)
swipeView.addView(recyclerView)
swipeView.setOnRefreshListener(this)
apply { layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) }
addView(swipeView)
}
/**
* 初始化配置
*/
fun initOptions(options: ConfigOptions) {
recyclerView.layoutManager = options.layoutManager()
// 对外提供 RecyclerView
options.configRecyclerView(recyclerView)
// 对外提供 SwipeRefreshLayout
options.configSwipeRefreshLayout(swipeView)
// 对内提供 Adapter
mAdapter = options.configAdapter()
initAdapter()
// 是否可刷新
setEnableRefresh(options.setEnableRefresh())
// 是否可加载
setEnableLoadMore(options.setEnableLoadMore())
}
/**
* 获取 Adapter
*/
fun getAdapter(): BaseQuickAdapter<*, BaseViewHolder> {
checkInited()
return mAdapter
}
// 获取 RecyclerView 实例
fun getRecyclerView(): RecyclerView = recyclerView
/**
* 获取 SwipeRefreshLayout 实例
*/
fun getSwipeRefreshLayout(): SwipeRefreshLayout = swipeView
fun setProgressBackgroundColorSchemeColor(color: Int) {
swipeView.setProgressBackgroundColorSchemeColor(color)
}
fun setBackgroudColor(color: Int) {
recyclerView.setBackgroundColor(color)
}
/**
* 设置 RecyclerView LayoutManager
*/
private fun setLayoutManager(layoutManager: RecyclerView.LayoutManager) {
recyclerView.layoutManager = layoutManager
}
/**
* 初始化 Adapter
*/
private fun initAdapter() {
// recyclerView.layoutManager ?: let { throw NullPointerException("Please invoke the setLayoutManager method first") }
mAdapter.setEnableLoadMore(isCanLoadMore)
mAdapter.setOnLoadMoreListener(this, recyclerView)
recyclerView.adapter = mAdapter
}
// baseAdapter 刷新方法
override fun onLoadMoreRequested() {
swipeView.isEnabled = false
if (swipeRefreshListener != null) {
swipeRefreshListener?.onLoadMoreRequested()
}
}
/**
* 设置是否可刷新
*/
fun setEnableRefresh(isCanRefresh: Boolean) {
this.isCanRefresh = isCanRefresh
swipeView.isEnabled = isCanRefresh
}
/**
* 设置是否可加载更多
*/
fun setEnableLoadMore(isCanLoadMore: Boolean) {
this.isCanLoadMore = isCanLoadMore
checkInited()
mAdapter.setEnableLoadMore(isCanLoadMore)
}
/**
* 设置空布局
*/
fun setEmptyView(view: View) {
checkInited()
mAdapter.emptyView = view
}
/**
* 设置空布局
*/
fun setEmptyView(layoutId: Int) {
checkInited()
mAdapter.setEmptyView(layoutId)
}
/**
* 加载到最后一页
*/
fun loadMoreEnd() {
checkInited()
mAdapter.loadMoreEnd()
reset()
}
/**
* 加载到最后一页
* 是否显示加载完成布局
*/
fun loadMoreEnd(gone: Boolean) {
checkInited()
mAdapter.loadMoreEnd(gone)
reset()
}
/**
* 当前页加载完成,非最后一页
*/
fun loadMoreComplete() {
checkInited()
mAdapter.loadMoreComplete()
reset()
}
/**
* 加载失败
*/
fun loadMoreFail() {
checkInited()
mAdapter.loadMoreFail()
reset()
}
// swipeRefreshLayout 刷新方法
override fun onRefresh() {
checkInited()
mAdapter.setEnableLoadMore(false)
if (swipeRefreshListener != null) {
swipeRefreshListener?.onRefresh()
}
}
private fun reset() {
checkInited()
mAdapter.setEnableLoadMore(isCanLoadMore)
swipeView.isEnabled = isCanRefresh
swipeView.isRefreshing = false
}
/**
*
*/
private fun checkInited() {
if (!::mAdapter.isInitialized) {
throw IllegalAccessException(" invoke the initOption method first please")
}
}
interface SwipeRefreshListener {
fun onRefresh()
fun onLoadMoreRequested()
}
interface ConfigOptions {
// 初始化 layoutmanager
fun layoutManager(): RecyclerView.LayoutManager
// 初始化 Adapter
fun configAdapter(): BaseQuickAdapter<*, BaseViewHolder>
// 获取当前 SwipeRefreshLayout
fun configSwipeRefreshLayout(refreshLayout: SwipeRefreshLayout) = Unit
// 获取当前的 RecyclerView
fun configRecyclerView(recyclerView: RecyclerView) = Unit
// 设置是否可刷新
fun setEnableRefresh(): Boolean = false
// 设置是否可加载更多
fun setEnableLoadMore(): Boolean = false
}
}
源码以及测试类地址
有问题请留言!