简体   繁体   English

在LayoutManger类型(网格-线性)之间进行切换时,更改Recyclerview行设计

[英]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.xmlvalues-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.

相关问题 Android Recyclerview:在单击按钮时将行从线性更改为网格 - Android Recyclerview : change row from linear to grid on button click 如何在jetpack compose(垂直,水平,网格)中为lazyColoumn更改layoutManger - How to change layoutManger for lazyColoumn in jetpack compose(vertical ,horizontal ,grid) 自动旋转设备时更改 RecyclerView 布局(从线性到网格和反向) - Change RecyclerView Layout (from Linear to Grid and reverse) when rotate device automatically 使用 MVVM 在 RecyclerView 的 Grid 和 Linear 之间切换的更好方法是什么? - What is a better way to switch between Grid and Linear of RecyclerView with MVVM? 观察用户时更改 RecyclerView 行 - Change RecyclerView Row When Observe User recyclerview 中更改行的问题 - issue in change row in recyclerview 当我单击线性布局以更改背景时,在Recyclerview中选择了两个项目 - Two Item get selected in Recyclerview when i click on Linear Layout to change Background Android - RecyclerView Grid对齐开始和结束但居中并且每行之间的空间相同 - Android - RecyclerView Grid aligned Start and end but centered and same space between each row Recyclerview始终具有备用1列网格行和2列网格行 - Recyclerview with alternate 1 column grid row and 2 columns grid row throughout 夹住Tab时如何更新碎片数据 - how to update fragement data when swiching Tabs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM