Android 中如何在 RecyclerView 中添加 LinearLayout Manager 和 GridLayout Manager [英] How to Add A LinearLayout Manager and GridLayout Manager in RecyclerView in Android
问题描述
我有一个设计需要像视图上的顶部图像一样实现,底部有一个带有两个列的 GridView
.所以我计划使用 RecyclerView
来实现它.对于顶部,我需要使用 LinearLayoutManager
和底部它有 GridLayoutManager
.那么如何使用这两个值创建 Recycler Adapter.
I had a design is needed to implement like a Top image on the View and bottom there is a GridView
with two coloumns. So I am Planning to implement it Using RecyclerView
. For top I need to use the LinearLayoutManager
and Bottom it has GridLayoutManager
. So How can I create a Recycler Adapter with these two values.
推荐答案
假设您知道如何创建具有多种视图类型的 RecyclerView 适配器,这就是您为 recyclerview 指定布局管理器的方式
Assuming you know how to create RecyclerView adapter with multiple view types, this is how you specify the layout manager for the recyclerview
mAdapter = new MyMultiViewAdapter(); // an imaginary adapter that supports multiple view types
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager glm = new GridLayoutManager(this, 2);
glm.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(glm);
mRecyclerView.setAdapter(mAdapter);
这篇关于Android 中如何在 RecyclerView 中添加 LinearLayout Manager 和 GridLayout Manager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!