StaggeredGridLayoutManager.如何在第一行中有一项,而在其他行中有2项 [英] StaggeredGridLayoutManager. How to have one item in first row and 2 item other rows

查看:800
本文介绍了StaggeredGridLayoutManager.如何在第一行中有一项,而在其他行中有2项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将recyclerview与staggeredGridLayoutManager一起使用. 我想做的是在第一行中显示一个标题项.那么我想在下面的每一行中显示2个项目.但是我无法这样做.

I am using recyclerview with staggeredGridLayoutManager. What i am trying to do is to show one header item in first row. then i want to show 2 items in each below row. But i am unable to do so.

这是我使用的代码.

    recyclerView = (RecyclerView)findViewById(R.id.recycler);
    staggeredGridLayoutManager = new StaggeredGridLayoutManager(1,1);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);

这些是结果

帮我解决这个问题.谢谢

Help me with this. thanks

推荐答案

您可以使用GridLayoutManager.

You could use GridLayoutManager.

GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);

manager.setSpanSizeLookup(
    new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
             // 2 column size for first row
             return (position == 0 ? 2 : 1);
        }
    });

在这里,我们正在创建具有2个网格列的GridLayoutManager.然后仅将第一行的跨度设置为2.

Here we are creating a GridLayoutManager with 2 grid columns. Then only for the first row, we are setting the span size to 2.

这篇关于StaggeredGridLayoutManager.如何在第一行中有一项,而在其他行中有2项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆