【问题标题】:RecyclerView inside NestedScrollView takes too much time when loading large amount of dataNestedScrollView里面的RecyclerView在加载大量数据时耗时太长
【发布时间】:2018-01-03 08:06:41
【问题描述】:

NestedScrollView 内的 RecyclerView 在加载大量数据时会冻结 Activity。使用 ScrollView 加载速度要快得多,但在这种情况下会影响滚动。 我尝试设置 setAutoMeasure 和 setNestedScrollingEnabled 等属性,但没有帮助。 有什么建议吗?

【问题讨论】:

  • 数据量大?比如多大?
  • @Wizard : 300 个条目
  • 你在加载什么?只有字符串?图片 ?每个单元格都有后台操作吗?没有更多细节很难帮助你。你能发布相关的 XML 和代码吗?
  • @kunal.c 你修好了?

标签: android android-recyclerview android-nestedscrollview


【解决方案1】:

据我了解,NestedScrollViews 中不支持回收视图,因此建议尝试更改布局。

【讨论】:

  • 我同意,遇到同样的问题,我的应用程序冻结了 2-3 秒。我收到了一些警告,指出 UI 线程正在做太多工作。删除 NestedScrollView 后,我没有冻结,也没有关于主线程的警告。
【解决方案2】:

这有助于提高回收站视图滚动的速度。

SpeedyLinearLayoutManager linearLayoutManager = new SpeedyLinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

myRecyclerView.setLayoutManager(linearLayoutManager);

SpeedyLinearLayoutManager.class

 public class SpeedyLinearLayoutManager extends LinearLayoutManager {

        private static final float MILLISECONDS_PER_INCH = 2f; //default is 25f (bigger = slower)

        public SpeedyLinearLayoutManager(Context context) {
            super(context);
        }

        public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

            final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
                }

                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                }
            };

            linearSmoothScroller.setTargetPosition(position);
            startSmoothScroll(linearSmoothScroller);
        }
    }

【讨论】:

    【解决方案3】:

    在滚动视图中使用自定义列表视图而不是回收器视图...

    Check this Answer

    自定义列表视图

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.ViewGroup;
    import android.widget.ListView;
    
    public class ExpandableHeightListView extends ListView {
    
    boolean expanded = true;
    public ExpandableHeightListView(Context context) {
        super(context);
    }
    public ExpandableHeightListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ExpandableHeightListView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }
    public boolean isExpanded() {
        return expanded;
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded()) {
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
    
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    
    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }
    }
    

    【讨论】:

      【解决方案4】:

      你可以加入app:layout_behavior="@string/appbar_scrolling_view_behavior"

      <android.support.v7.widget.RecyclerView
          android:id="@+id/recyclerView"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      

      或禁用回收器视图的嵌套滚动行为。

      recyclerView.setNestedScrollingEnabled(false);
      

      【讨论】:

      • 谢谢,但这些都没有帮助!
      猜你喜欢
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多