【问题标题】:RecyclerView with disabled scrolling is not efficient禁用滚动的 RecyclerView 效率不高
【发布时间】:2018-02-11 16:15:46
【问题描述】:

我需要一个具有以下功能的高效 RecyclerView:

  • 嵌套在滚动视图中,与其他视图一起滚动
  • 填充渲染所有项目所需的所有(估计)空间
  • 仅渲染可见项目并重复使用以前可见的视图。

目前我可以通过 LayoutManager 禁用滚动来实现功能 1 和 2:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
TasksListView().setLayoutManager(linearLayoutManager);

但在这种情况下,所有项目都将在开始时间呈现。显然这是性能问题。

我如何对 RecyclerView 说只为所有项目保留估计空间(使用所有项目的计数)并使用以前创建的 ViewHolders 呈现项目?

【问题讨论】:

  • 你想在你的数据之间留一个空格吗?
  • 我也有同样的要求。你有什么解决办法吗?请让我知道你是如何做到这一点的。

标签: android android-recyclerview recyclerview-layout


【解决方案1】:

试试这个代码:

import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;

import android.view.View;


public abstract class RecyclerViewByNestedScrollListener implements NestedScrollView.OnScrollChangeListener {
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;
    // The minimum amount of pixels to have below your current scroll position
    // before loading more.
    private int visibleThresholdDistance = 500;

    RecyclerView.LayoutManager mLayoutManager;

    public RecyclerViewByNestedScrollListener(RecyclerView.LayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
    }

    @Override
    public void onScrollChange(NestedScrollView scrollView, int x, int y, int oldx, int oldy) {
        // We take the last son in the scrollview
        View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
        int distanceToEnd = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

        int totalItemCount = mLayoutManager.getItemCount();
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) {
                this.loading = true;
            }
        }

        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        // threshold should reflect how many total columns there are too
        if (!loading && distanceToEnd <= visibleThresholdDistance) {
            currentPage++;
            onLoadMore(currentPage, totalItemCount);
            loading = true;
        }
    }

    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int page, int totalItemsCount);

}

然后使用 NestedScrollView 作为你的 recyclerView 的父级。然后在代码中这样做:

LinearLayoutManager layoutManager=new LinearLayoutManager(this);

NestedScrollView mScrNested=(NestedScrollView)findViewById(R.id.nestedScrollView);

mScrNested.setOnScrollChangeListener(new RecyclerViewByNestedScrollListener(layoutManager) {
                @Override
                public void onLoadMore(int page, int totalItemsCount) {
                    //load next page of your recyclerView
                }
            });

正如您在上面看到的,您必须使用nestedScorllView 设置加载更多的recyclerview。否则您的数据会立即加载并且您的视图已经滞后。 所有这些问题都是因为 recyclerView 在 ScrollView 中无法正常工作。

【讨论】:

  • 我使用了代码,但是只显示了 5 个项目,而其他所有项目(超过 30 个项目)都将被渲染。
  • 此方法适用于nestedScrollView 末尾屏幕的可见像素(500 像素)。在您的请求的每个页面上获取多少数据?
  • 我有 40 条记录作为样本,只显示 4 条(和第 5 条的一小部分)。我的屏幕高度是 1920,recyclerview 接近 1300 像素,这意味着每个项目大约需要 400 像素,所以如果项目 6 和 7 将被渲染,代码可以正常工作,但最后的所有项目都将被渲染(~12.000 像素)跨度>
  • 你使用 setNestedScrollingEnabled(false);为您的 RecyclerView 和 android:fillViewport="true" 为您的 NestedScrollView?
  • 是的,两者都已设置,并且无论可见性如何都会呈现所有项目
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 2020-11-10
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多