[英]Change Recyclerview row design when swiching between LayoutManger types (Grid - Linear)
I'm trying to learn more about RecyclerView and its implementations, so in this case I need to to design tow item views, one will show when LayoutManager is LinearLayoutManager which in this case will show a vertical List of cards, the second item design will be used when changing LayoutManager to GridLayoutManager type. 我试图了解有关RecyclerView及其实现的更多信息,因此在这种情况下,我需要设计两个项目视图,一个将在LayoutManager为LinearLayoutManager时显示,在这种情况下将显示垂直卡列表,第二个项目设计将在将LayoutManager更改为GridLayoutManager类型时使用。
my question is how to implement such logic?
我的问题是如何实现这样的逻辑?
I read some answers here, like this about Recyclerview with multiple row design but found nothing about switching between LayoutManager types. 我在这里读了一些答案,像这样大约Recyclerview多排设计,但没有发现任何关于布局管理类型之间进行切换。 and according to this talk we shouldn't play with layout manager in Adapter.
根据这个演讲,我们不应该在Adapter中使用布局管理器。
finally I have to mention that I'm new to Android development so I hope my question is clear, and I appreciate any help. 最后,我不得不提到我是Android开发的新手,所以我希望我的问题很清楚,并且感谢您的帮助。
If you wanna change between vertical linear layout and grid (vertical) layout, just use GridLayoutManager
and change the column span between 1 (vertical layout) and a value higher than 1 (for grid). 如果要在垂直线性布局和网格(垂直)布局之间进行更改,只需使用
GridLayoutManager
并将列范围更改为1(垂直布局)和大于1的值(对于网格)即可。
You can use different span values for different layouts using the layout qualified resource folders. 您可以使用符合布局要求的资源文件夹为不同的布局使用不同的跨度值。
For instance, imagine you want vertical linear in portrait and grid in land: 例如,假设您要在纵向中使用垂直线性,在陆地中使用网格:
int spanCount = getResources().getInteger(R.integer.column_count);
return new GridLayoutManager(context, spanCount);
Then create values/integer.xml
and values-land/integers.xml
where you will define column_count
with the different values. 然后创建
values/integer.xml
和values-land/integers.xml
,在其中您将使用不同的值定义column_count
。
you can create both layout managers and set the right manager to your RecyclerView in the activity and not the adapter according to a certain condition (screen orientation for example): 您可以同时创建布局管理器,并根据特定条件(例如,屏幕方向)在活动中(而不是在适配器中)为RecyclerView设置正确的管理器:
@Override
protected void onCreate(Bundle savedInstanceState) {
RecyclerView mRecyclerView
int numberOfColumns = 3;
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_here);
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false);
GridLayoutManager mGridLayoutManager = new GridLayoutManager(this,numberOfColumns);
mRecyclerView = (RecyclerView) findViewById(R.id.your_view_recycler)
if (condition){
mRecyclerView.setLayouManager(mLinearLayoutManager)
} else {
mRecyclerView.setLayoutManager(mGridLayoutManager)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.