GridLayoutManager Customization

梦想与她 提交于 2021-02-10 05:49:30

问题


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

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

something like this:

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 appreciated thanks


回答1:


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


来源:https://stackoverflow.com/questions/46444900/gridlayoutmanager-customization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!