本文介绍了GridLayoutManager自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在android中自定义 gridlayoutmanager 使其具有水平布局,例如此草图:

i want to know if there is way that i can customize gridlayoutmanager in android to have horizontal layout like this sketch:

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

i tried some layout manager that i found in Github but non of them that help me to implement this kind of layout manager.

类似这样的东西:

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);
}}

任何帮助将不胜感激谢谢

any help will be appreciatedthanks

推荐答案

我认为您不需要 CustomLayoutManager .对您的 RecyclerView 执行此操作:

I don't think you need the CustomLayoutManager. Do this to your 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);

这篇关于GridLayoutManager自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:10