There is an Activity with ViewPager . In one of the Page there is a fragment with RecyclerView . There I set both the LayoutManager and ScrollListener in the onCreateView method. All is well. I android:configChanges="screenSize" on android:configChanges="screenSize" , in order to prevent it from being recreated when changing orientation. So, how to change the orientation again call LayoutManager , etc,? For example, GridAutofitLayoutManager columns were calculated in the GridAutofitLayoutManager ? tried this:

 public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Integer minWidth; minWidth = (int) getResources().getDimension(R.dimen.minItemWidht); Log.d("TAG ", String.valueOf(minWidth)); layoutManager = new GridAutofitLayoutManager(context, minWidth); mRecyclerView.setLayoutManager(layoutManager); } 

But it did not help. During debug everything is fine, GridAutofitLayoutManager calculates the number of new columns. As soon as you remove the stop point, it stops. Perhaps not enough time?

Here is the code GridAutofitLayoutManager

totalSpice somehow sometimes incorrectly calculates

    2 answers 2

    Use a custom RecyclerView ( public class AutofitRecyclerView extends RecyclerView ) And in it you can recalculate the amount in the onMeasure method.

    This code works well:

     package сom.example; import android.content.Context; import android.content.res.TypedArray; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; public class AutofitRecyclerView extends RecyclerView { int spanCount = 1; private GridLayoutManager manager; private int columnWidth = -1; public AutofitRecyclerView(Context context) { super(context); init(context, null); } public AutofitRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } private void init(Context context, AttributeSet attrs) { if (attrs != null) { int[] attrsArray = { android.R.attr.columnWidth }; TypedArray array = context.obtainStyledAttributes(attrs, attrsArray); columnWidth = array.getDimensionPixelSize(0, -1); array.recycle(); } manager = new GridLayoutManager(getContext(), 5); manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { switch (getAdapter().getItemViewType(position)) { case AppsAdapter.TYPE_HEADER: return spanCount; case AppsAdapter.TYPE_ITEM: return 1; default: return 1; } } }); setLayoutManager(manager); } @Override public GridLayoutManager getLayoutManager() { return manager; } @Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); if (columnWidth > 0) { spanCount = Math.max(1, getMeasuredWidth() / columnWidth); manager.setSpanCount(spanCount); } } } 

      Helped me

        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); Integer minWidth; minWidth = (int) getResources().getDimension(R.dimen.minItemWidht); layoutManager = new GridAutofitLayoutManager(context, minWidth); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(layoutManager); adapterRecycler.notifyDataSetChanged(); } }); 
      • Only sizes went crazy - Konstantin