使用禁用滚动的RecyclerView效率不高

时间:2018-02-11 07:19:00

标签: android android-recyclerview recyclerview-layout

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

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

目前我可以实现功能1& 2,禁用滚动浏览LayoutManager:

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

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

我怎么能说RecyclerView只保留所有项目的估计空间(使用所有项目的计数)并使用以前创建的ViewHolders渲染项目?

1 个答案:

答案 0 :(得分:0)

试试这段代码:

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加载。否则,您的数据会立即加载,并且您的视图会滞后。 所有这些问题都是因为recyclelerView在ScrollView中无法正常工作。