RecyclerView的GridLayoutManager - 交替的1和2列行

时间:2016-12-10 12:31:50

标签: android android-layout android-recyclerview gridlayoutmanager

我想要第一行中的1张图像和第二行中的2张图像,然后重复等等 我希望recyclerview像这样

screenshot

3 个答案:

答案 0 :(得分:0)

你应该这样做:

mRecyclerView = (RecyclerView) view.findViewById(R.id.grid);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch(mAdapter.getItemViewType(position)){
                case MyAdapter.TYPE_HEADER:
                    return 2;
                case MyAdapter.TYPE_ITEM:
                    return 1;
                default:
                    return -1;
            }
        }
    });

mRecyclerView.setLayoutManager(mLayoutManager);

并将商品的类型设置为适配器中的标题或项目

答案 1 :(得分:0)

    GridLayoutManager layout_manager = new GridLayoutManager(mContext, 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (position % 3 == 0 ? 2 : 1);
            }
        });

/* Custom Adapter */

        Adapter contact_adapter = new Adapter(MainActivity.this, all_contacts_list);
        recyclerView.setLayoutManager(layout_manager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(contact_adapter);

答案 2 :(得分:-1)

RecyclerView mRecyclerView =
    (RecyclerView) findViewById(R.id.recycler_view);
YourAdapter yourAdapter = new YourAdapter(this, arrayList);
if (mRecyclerView != null) {
  mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
  mRecyclerView.setAdapter(yourAdapter);
}