6

RecyclerView inside NestedScrollView freezes the Activity when loading large amount of data. It loads much faster with ScrollView, but scrolling is affected in that case. I tried setting attributes like setAutoMeasure and setNestedScrollingEnabled which did not help. Any suggestions?

kunal.c
  • 2,739
  • 1
  • 18
  • 24

4 Answers4

2

Recycling of views is not supported inside NestedScrollViews as I understand, so the suggestion would be to try to change your layout.

David
  • 3,971
  • 1
  • 26
  • 65
  • I agree, faced the same issue, my app was freezing for 2-3 seconds. I was receiving some warning stating that the UI thread was doing too much work. Once I removed the NestedScrollView I had no freeze and no warnings concerning the main thread. – Shid Mar 25 '20 at 23:01
  • @Shid which layout did you used instead of nestedscrollview? – V Surya Kumar May 26 '22 at 21:16
0

This help to increase the speed of recycler view scrolling.

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);
        }
    }
jessica
  • 1,700
  • 1
  • 11
  • 17
-1

Use Custom list view instead of Recycler view inside scroll view...

Check this Answer

Custom List View

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;
}
}
BarmanInfo
  • 392
  • 1
  • 18
-2

you can include 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" />

or disable the nested scrolling behavior for the recycler view.

recyclerView.setNestedScrollingEnabled(false);
SaravInfern
  • 3,338
  • 1
  • 20
  • 44