示例
使用gridlayout布局管理器创建recyclerview时,必须在构造函数中指定跨度计数。跨度计数是指列数。这相当笨拙,并且没有考虑更大的屏幕尺寸或屏幕方向。一种方法是为各种屏幕尺寸创建多个布局。下面可以看到另一种更动态的方法。
首先,我们创建一个自定义的RecyclerView类,如下所示:
public class AutofitRecyclerView extends RecyclerView {
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(), 1);
setLayoutManager(manager);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
if (columnWidth > 0) {
int spanCount = Math.max(1, getMeasuredWidth() / columnWidth);
manager.setSpanCount(spanCount);
}
}
}
此类确定可以在recyclerview中容纳多少列。要使用它,您需要将其放入您的计算机中layout.xml,如下所示:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/auto_fit_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="200dp"
android:clipToPadding="false"
/>
注意,我们使用columnWidth属性。recyclerview将需要它来确定可容纳可用空间的列数。
在您的活动/片段中,您仅获得对recylerview的引用,并为其设置适配器(以及要添加的任何物品装饰或动画)。不要设置版面管理器
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.auto_fit_recycler_view);
recyclerView.setAdapter(new MyAdapter());
(其中MyAdapter是您的适配器类)
现在,您将拥有一个recyclerview,该视图将调整跨度计数(即列)以适合屏幕尺寸。作为最后的补充,您可能想将列在recyclerview中居中(默认情况下,它们与layout_start对齐)。您可以通过稍微修改AutofitRecyclerView类来实现。首先在recyclerview中创建一个内部类。这是从GridLayoutManager扩展的类。它将在左侧和右侧添加足够的填充以使行居中:
public class AutofitRecyclerView extends RecyclerView {
// 等见上面
private class CenteredGridLayoutManager extends GridLayoutManager {
public CenteredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CenteredGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public CenteredGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
@Override
public int getPaddingLeft() {
final int totalItemWidth = columnWidth * getSpanCount();
if (totalItemWidth >= AutofitRecyclerView.this.getMeasuredWidth()) {
return super.getPaddingLeft(); // 没做什么
} else {
return Math.round((AutofitRecyclerView.this.getMeasuredWidth() / (1f + getSpanCount())) - (totalItemWidth / (1f + getSpanCount())));
}
}
@Override
public int getPaddingRight() {
return getPaddingLeft();
}
}
}
然后,当您在AutofitRecyclerView中设置LayoutManager时,请使用CenteredGridLayoutManager,如下所示:
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 CenteredGridLayoutManager(getContext(), 1);
setLayoutManager(manager);
}
就是这样!您有一个动态的跨度,基于中心的gridlayoutmanager基于recyclerview。
资料来源:陈秋琪的方岛博客
堆栈溢出