如果您RecyclerView从网络中加载数据中的项目(通常是图像)或执行其他处理,这可能会花费大量时间,并且最终可能会在屏幕上显示项目,但未完全加载。为避免这种情况,您可以扩展现有LinearLayoutManager项以在屏幕上可见之前预加载许多项:
package com.example; import android.content.Context; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; /** * A LinearLayoutManager that preloads items off-screen. * <p> * Preloading is useful in situations where items might take some time to load * fully, commonly because they have maps, images or other items that require * network requests to complete before they can be displayed. * <p> * By default, this layout will load a single additional page's worth of items, * a page being a pixel measure equivalent to the on-screen size of the * recycler view. This can be altered using the relevant constructor, or * through the {@link #setPages(int)} method. */ public class PreLoadingLinearLayoutManager extends LinearLayoutManager { private int mPages = 1; private OrientationHelper mOrientationHelper; public PreLoadingLinearLayoutManager(final Context context) { super(context); } public PreLoadingLinearLayoutManager(final Context context, final int pages) { super(context); this.mPages= pages; } public PreLoadingLinearLayoutManager(final Context context, final int orientation, final boolean reverseLayout) { super(context, orientation, reverseLayout); } @Override public void setOrientation(final int orientation) { super.setOrientation(orientation); mOrientationHelper = null; } /** * Set the number of pages of layout that will be preloaded off-screen, * a page being a pixel measure equivalent to the on-screen size of the * recycler view. * @param pages the number of pages; can be {@code 0} to disable preloading */ public void setPages(final int pages) { this.mPages= pages; } @Override protected int getExtraLayoutSpace(finalRecyclerView.Statestate) { if (mOrientationHelper == null) { mOrientationHelper = OrientationHelper.createOrientationHelper(this, getOrientation()); } return mOrientationHelper.getTotalSpace() * mPages; } }