youlookwhat / ByRecyclerView

🔥 RecyclerView 下拉刷新、上拉松手/自动加载更多、item点击/长按、item局部刷新、头布局/尾布局/状态布局、万能分割线、Skeleton骨架图、极简adapter、嵌套滑动置顶
https://youlookwhat.github.io/ByRecyclerView
Apache License 2.0
798 stars 139 forks source link

作者你好,空item的视图希望加个自动化吧,自己手动隐藏麻烦 #51

Closed chengzhuangjie closed 1 year ago

youlookwhat commented 2 years ago

是的,我也觉得挺麻烦的,我处理一下,可能要等个两三天。 你有好的方案吗,可以提requests,或者直接发给我,我加上去,这样可能会快点。

renhuan commented 1 year ago

我写了一个扩展函数自动完成下拉刷新,上拉加载,和空布局,还有啥好的建议吗?

fun <T> BaseByRecyclerViewAdapter<T, *>.finish(
    data: List<T>?,
    emptyLayoutId: Int = R.layout.layout_empty
) {
    if (recyclerView.isRefreshing || this.data.isNullOrEmpty()) {
        recyclerView.isRefreshing = false
        if (data.isNullOrEmpty()) {
            recyclerView.setEmptyView(emptyLayoutId)
            recyclerView.setNotFullScreenNoLoadMore()
            recyclerView.setEmptyViewEnabled(true)
            recyclerView.isLoadMoreEnabled = false
            setNewData(null)
        } else {
            recyclerView.setEmptyViewEnabled(false)
            recyclerView.isLoadMoreEnabled = true
            setNewData(data)
        }
    } else {
        recyclerView.loadMoreComplete()
        if (data.isNullOrEmpty()) {
            recyclerView.loadMoreEnd()
        } else {
            addData(data)
        }
    }
}

接口请求完成后调用 mAdapter.finish(datas)

youlookwhat commented 1 year ago

厉害厉害,我看能不能封装在库里~

youlookwhat commented 1 year ago

@chengzhuangjie 处理空视图其实只有三行代码:

mRecyclerView.setStateView(emptyLayout);
mRecyclerView.setLoadMoreEnabled(false);
setNewData(null);

同样,显示数据也是三行代码:

mRecyclerView.setLoadMoreEnabled(true);
mRecyclerView.setStateViewEnabled(false);
setNewData(data);

可能是我demo里没写清晰,我完善一下

youlookwhat commented 1 year ago

@renhuan 你那个 如果是第一页和进来不是自动下拉刷新,感觉是有点问题的,我完善了一下:

    /**
     * 设置数据和处理空视图
     */
    public void setPageData(boolean isFirstPage, List<T> data, View emptyLayout) {
        if (mRecyclerView == null) {
            return;
        }
        if (isFirstPage) {
            if (data != null && data.size() > 0) {
                mRecyclerView.setLoadMoreEnabled(true);
                mRecyclerView.setStateViewEnabled(false);
                setNewData(data);
            } else {
                mRecyclerView.setStateView(emptyLayout);
                mRecyclerView.setLoadMoreEnabled(false);
                setNewData(null);
            }
        } else {
            if (data != null && data.size() > 0) {
                addData(data);
                mRecyclerView.loadMoreComplete();
            } else {
                mRecyclerView.loadMoreEnd();
            }
        }
    }

我提交了再发一版~

youlookwhat commented 1 year ago

@renhuan 等发布后,你可以再弄了扩展函数,将setPageData()里的emptyLayout固定一下,就用的比较顺手了

renhuan commented 1 year ago

@youlookwhat 好的,有时间我用一下

youlookwhat commented 1 year ago

@chengzhuangjie @renhuan 依赖最新的包:implementation 'com.github.youlookwhat:ByRecyclerView:1.3.5' adapter使用这个方法即可:setPageData(boolean isFirstPage, List<T> data, int emptyLayoutId)

youlookwhat commented 1 year ago

建议使用最新的包:implementation 'com.github.youlookwhat:ByRecyclerView:1.3.6'

暂时关闭了,有问题再提了 @chengzhuangjie @renhuan