recyclerview设置行数,改变RecyclerView网格布局列数

在Android中,为了根据屏幕尺寸动态调整RecyclerView的网格布局列数,可以通过测量视图宽度和卡片视图宽度来计算新的列数。当屏幕尺寸变化时,可以使用RecyclerView的TreeViewObserver。此外,可以通过创建一个自适应宽度的RecyclerView子类,在onMeasure方法中根据实际宽度设置GridLayoutManager的spanCount。
摘要由CSDN通过智能技术生成

I am trying to change the number of columns that appear in the recycler view (grid layout) based on the display size. However I couldn't figure out a proper way of achieving it. At the moment I am using treeViewObserver to change the number of columns based the change in screen size (during orientation). So if the app opens in portrait mode, number of columns (on the phone) it decides to be one, which look good but this method doesn't work when the app directly opens in landscape mode where a single streched out card in the grid is displayed on the screen.

Here recList is RecyclerView & glm is GridLayoutManager used in RecyclerView

viewWidth = recList.getMeasuredWidth();

cardViewWidthZZ = recList.getChildAt(0).getMeasuredWidth();

if (oldWidth == 0) {

oldWidth = cardViewWidthZZ;

}

if (oldWidth <= 0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值