Grid Recyclerview,偶数行中有3列,奇数行中有4列(Grid Recyclerview with 3 columns in even number row and 4 columns

编程入门 行业动态 更新时间:2024-10-25 02:22:58
Grid Recyclerview,偶数行中有3列,奇数行中有4列(Grid Recyclerview with 3 columns in even number row and 4 columns in odd number row)

如何创建一个Grid Recyclerview,其中偶数行为3列,奇数行为4列?

lLayout = new GridLayoutManager(getActivity(), 4, LinearLayoutManager.VERTICAL, false); // MAX NUMBER OF SPACES lLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (position % 3 == 0 ? 3 : 4); } }); recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); recyclerView.setLayoutManager(lLayout); if (arrayList != null) { adapter = new RecyclerViewAdapter(getActivity(), arrayList); recyclerView.setAdapter(adapter); }

How to create a Grid Recyclerview with 3 columns in even number row and 4 columns in odd number row?

lLayout = new GridLayoutManager(getActivity(), 4, LinearLayoutManager.VERTICAL, false); // MAX NUMBER OF SPACES lLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (position % 3 == 0 ? 3 : 4); } }); recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); recyclerView.setLayoutManager(lLayout); if (arrayList != null) { adapter = new RecyclerViewAdapter(getActivity(), arrayList); recyclerView.setAdapter(adapter); }

最满意答案

尝试这个,

// Create a grid layout with 12 columns // (least common multiple of 3 and 4) GridLayoutManager manager = new GridLayoutManager(this, 12, GridLayoutManager.VERTICAL, false); manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { // 7 is the sum of items in one repeated section switch (position % 7) { // first three items span 3 columns each case 0: case 1: case 2: return 4; // next four items span 2 columns each case 3: case 4: case 5: case 6: return 3; } throw new IllegalStateException("internal error"); } }); recyclerView.setLayoutManager(manager);

看到这个:

Try this,

// Create a grid layout with 12 columns // (least common multiple of 3 and 4) GridLayoutManager manager = new GridLayoutManager(this, 12, GridLayoutManager.VERTICAL, false); manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { // 7 is the sum of items in one repeated section switch (position % 7) { // first three items span 3 columns each case 0: case 1: case 2: return 4; // next four items span 2 columns each case 3: case 4: case 5: case 6: return 3; } throw new IllegalStateException("internal error"); } }); recyclerView.setLayoutManager(manager);

See this:

更多推荐

本文发布于:2023-08-01 19:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   奇数   偶数   Recyclerview   Grid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!