GridLayoutManager 自定义

GridLayoutManager Customization

我想知道是否有方法可以在 android 中自定义 gridlayoutmanager 以具有如下草图的水平布局:

我尝试了一些在 Github 中找到的布局管理器,但没有一个能帮助我实现这种布局管理器。

像这样:

public class CustomLayoutManager extends StaggeredGridLayoutManager {
public CustomLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public CustomLayoutManager(int spanCount, int orientation) {
    super(spanCount, orientation);
}

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
    super.onMeasure(recycler, state, widthSpec, heightSpec);
}

@Override
public void setSpanCount(int spanCount) {
    super.setSpanCount(spanCount);
}}

任何帮助将不胜感激 谢谢

我认为您不需要 CustomLayoutManager。对您的 RecyclerView:

执行此操作
GridLayoutManager layoutManager = new GridLayoutManager(itemView.getContext(), 2, GridLayoutManager.HORIZONTAL, false);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        return position == 0 ? 2 : 1;//put your condition here
    }
});
recyclerView.setLayoutManager(layoutManager);