美文网首页
封装分页下拉刷新:PullRefreshRecyclerView

封装分页下拉刷新:PullRefreshRecyclerView

作者: Lei_9c47 | 来源:发表于2021-07-25 12:42 被阅读0次

    基于SmartRefreshLayout+RecyclerView封装,使得开发者不再需要自己在项目中频繁处理分页记录等繁琐状态。
    github地址
    使用方法:

    <com.gl.gllibrary.widget.pullrefersh.PullRefreshRecyclerView
            android:id="@+id/mPullRefreshRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:autoLoadMore="true"
            app:autoRefresh="true"
            app:gl_layoutManager="StaggeredGrid"
            app:gl_spanCount="1"
            app:gl_layoutOritation="VERTICAL"
            app:gl_reverseLayout="false" />
    

    可以在xml中定义layoutManager的各种属性,自动加载等。

    代码中可以设置默认加载个数,起始页,以及空态错误页面

            val emptyView = EmptyView(this)
            emptyView.butRetry.setOnClickListener {
                mPullRefreshRecyclerView.mRefreshPageBean.current_page_index = 1
                requestData(true, mPullRefreshRecyclerView.mRefreshPageBean.current_page_index)
            }
            mPullRefreshRecyclerView.mRefreshPageBean.page_load_size = 20
            mPullRefreshRecyclerView.setEmptyView(emptyView)
            mPullRefreshRecyclerView.refreshListener = this
            mPullRefreshRecyclerView.mRefreshPageBean.first_page_index = 540
            adapter = ArtListAdapter()
            mPullRefreshRecyclerView.setAdapter(adapter)
    

    空态页面继承PullRefreshEmptyView根据状态显示数据

    public class EmptyView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : PullRefreshEmptyView(context, attrs, defStyleAttr) {
    
        init {
            LayoutInflater.from(context).inflate(R.layout.empty_view, this, true)
        }
    
        override fun onEmptyStatus(status: Int) {
            if (status == RefreshPageBean.SHOW_EMPTY_STATUS) {
                tvErrorMsg.text = "暂无数据"
            } else if (status == RefreshPageBean.SHOW_EMPTY_ERROR_STATUS) {
                tvErrorMsg.text = "当前页面加载失败"
            }
        }
    }
    

    请求网络回调中调用finishLoadRequest,传入是否成功,下拉刷新,加载更多区分,并且在onDataNotifyChanged刷新即可

     fun requestData(isRefresh: Boolean, page: Int) {
            HttpClient.getRetrofit().create(WanAndroidApi::class.java)
                .articleList(page)
                .enqueue(object : Callback<ArticleListResult> {
                    override fun onResponse(
                        call: Call<ArticleListResult>,
                        response: Response<ArticleListResult>
                    ) {
                        val result = response.body() as ArticleListResult
                        mPullRefreshRecyclerView.finishLoadRequest(isRefresh,true, result.mData.datas,false)
                    }
    
                    override fun onFailure(call: Call<ArticleListResult>, t: Throwable) {
                        mPullRefreshRecyclerView.finishLoadRequest(isRefresh,false)
                    }
    
                })
        }
    
        override fun onRefresh() {
            requestData(true, mPullRefreshRecyclerView.mRefreshPageBean.current_page_index)
        }
    
        override fun onLoadMore() {
            requestData(false, mPullRefreshRecyclerView.mRefreshPageBean.current_page_index)
        }
    
        override fun onDataNotifyChanged(list: MutableList<Any>,isAllLoaded:Boolean) {
            if (isAllLoaded){
                Toast.makeText(this,"没有更多数据",Toast.LENGTH_SHORT).show()
            }
            adapter.mData = list as MutableList<DataX>
            adapter.notifyDataSetChanged()
        }
    
    ezgif.com-gif-maker.gif

    相关文章

      网友评论

          本文标题:封装分页下拉刷新:PullRefreshRecyclerView

          本文链接:https://www.haomeiwen.com/subject/ffnbmltx.html