主題:android
我試圖解決如下:使用RecyclerView與GridLayoutManager固定元格寬度RecyclerView只調整必要的高度(wrap_content)我嘗試使用如下代碼,問題是,它不工做,我尚未找到任何工做示例說起從新劃分不當當取向變化。git
原文:github
I try to solve the following:app
I try to use the following code, the problem is that it doesn't work, and I haven't found any working example not mentioning to redraw properly when orientation changes.ide
public class AutofitRecyclerView extends RecyclerView { private MyGridLayoutManager 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 MyGridLayoutManager(getContext(), 1); setLayoutManager(manager); } @Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); if (columnWidth > 0) { int mw = getMeasuredWidth(); int spanCount = Math.max(1, mw / columnWidth); manager.setSpanCount(spanCount); } int numOfFullRows = manager.getItemCount() / manager.getSpanCount(); if(manager.getItemCount() % manager.getSpanCount() > 0) { numOfFullRows++; } if(getChildCount() > 0) { int h = 0; try { h = manager.getChildAt(0).getMeasuredHeight(); } catch (Exception e) { e.printStackTrace(); } if(h != 0) { getLayoutParams().height = numOfFullRows * h; } } } }
解決方案:
你須要添加一個自定義GridlayoutManager。檢查這個https://gist.github.com/c0nnector/5f80e19d9ba6d562fbd5原文:測試
You'll need to add a custom GridlayoutManager. Check this https://gist.github.com/c0nnector/5f80e19d9ba6d562fbd5this
解決方案:
若是你設置如下屬性,那麼全部細胞都有固定大小:recyclerView.setHasFixedSize(真正的);原文:spa
If you set the below property, then all the cell will have fixed size: recyclerView.setHasFixedSize(true);code
解決方案:
基於代碼鏈接器發表我想出瞭如下解決方案,支持不一樣的行/列大小(儘管我誠然不測試),考慮了項目的大小裝飾,妥善處理不一樣的測量模式。這是代碼:原文:get
Based on the code that connector published I came up with the following solution that supports different row / column sizes (though I have admittedly not tested that), takes the sizes of the item decorations into account and handles the different measure modes properly.
Here is the code:
import android.content.Context; import android.graphics.Rect; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; public class WrappableGridLayoutManager extends GridLayoutManager { public WrappableGridLayoutManager(Context context, int spanCount) { super(context, spanCount); } private int[] measuredSize = new int[2]; @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { final int widthMode = View.MeasureSpec.getMode(widthSpec); final int heightMode = View.MeasureSpec.getMode(heightSpec); final int widthSize = View.MeasureSpec.getSize(widthSpec); final int heightSize = View.MeasureSpec.getSize(heightSpec); int spanWidth = 0; int spanHeight = 0; int viewWidth = 0; int viewHeight = 0; for (int i = 0; i < getItemCount(); i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize); if (i % getSpanCount() == 0) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } spanWidth = measuredSize[0]; spanHeight = measuredSize[1]; } else { if (getOrientation() == VERTICAL) { spanWidth += measuredSize[0]; spanHeight = Math.max(spanHeight, measuredSize[1]); } else { spanWidth = Math.max(spanWidth, measuredSize[0]); spanHeight += measuredSize[1]; } if (i == getSpanCount() - 1) { if (getOrientation() == VERTICAL) { viewWidth = Math.max(viewWidth, spanWidth); viewHeight += spanHeight; } else { viewWidth += spanWidth; viewHeight = Math.max(viewHeight, spanHeight); } } } } int finalWidth; int finalHeight; switch (widthMode) { case View.MeasureSpec.EXACTLY: finalWidth = widthSize; break; case View.MeasureSpec.AT_MOST: finalWidth = Math.min(widthSize, viewWidth); break; case View.MeasureSpec.UNSPECIFIED: finalWidth = viewWidth; break; default: finalWidth = widthSize; break; } switch (heightMode) { case View.MeasureSpec.EXACTLY: finalHeight = heightSize; break; case View.MeasureSpec.AT_MOST: finalHeight = Math.min(heightSize, viewHeight); break; case View.MeasureSpec.UNSPECIFIED: finalHeight = viewHeight; break; default: finalHeight = heightSize; break; } setMeasuredDimension(finalWidth, finalHeight); } private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { View view = recycler.getViewForPosition(position); if (view != null) { RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width); int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height); view.measure(childWidthSpec, childHeightSpec); measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; Rect decoratorRect = new Rect(); calculateItemDecorationsForChild(view, decoratorRect); measuredDimension[0] += decoratorRect.left; measuredDimension[0] += decoratorRect.right; measuredDimension[1] += decoratorRect.top; measuredDimension[1] += decoratorRect.bottom; recycler.recycleView(view); } } }