RecylerView布局管理器LayoutManager(详解)

设置布局管理器方法,实现RecylerView布局里面的内容显示方式;

recyclerView.setLayoutManager(LayoutManager layoutManager)

使用三个类

  • LinearLayoutManager:线性布局管理器
  • StaggeredGridLayoutManager: 错列网格布局管理器
  • GridLayoutManager:网格布局管理器

一、LayoutMananger子类LinearLayoutManager

一般使用LinearLayoutManager的两种构造方法
第一种构造方法:

new LinearLayoutManager(Context context)

参数为上下文环境,实现的是默认的垂直布局

第二种构造方法:

new LinearLayoutManager( Context context, int orientation, boolean reverseLayout)

第一个参数为上下文环境,第二个参数为布局显示方式,第三个参数为布尔值是否反转

垂直布局不反转实现方式有两种

LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
LinearLayoutManager layoutManager1=new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager1);

二、LayoutManager的子类StaggeredGridLayoutManager

new StaggeredGridLayoutManager(int spanCount,int orientation)
使用错列的布局,指定两个参数,一个是要显示的列数spanCount,一个是显示的方向orientation;

1、垂直Vertical实现2列显示方式:

StaggeredGridLayoutManager staggered=newStaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggered);

三、LinearLayoutManager 的子类GridLayoutManager

GridLayoutManager更像是LinearLayoutManagerStaggeredGridLayoutManager结合体,可以设置列数,又可以设置显示方向以及设置页面的加载数据是否反转
GridLayoutManager两种构造方法
第一种构造方法:

new GridLayoutManager(Context context, int spanCount)
第一个参数为上下文环境,第二个显示列数,默认显示垂直布局

第二种构造方法:

new GridLayoutManager( Context context, int spanCount, int orientation, boolean reverseLayout)
第一个参数为上下文环境,第二个显示列数,第三个参数为方向,第四个参数为是否反转

1、实现三列垂直Vertical布局

recyclerView.setLayoutManager(gridLayoutManager2);

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个 SWT 的示例代码,演示如何创建和设置 GridLayout 布局管理器: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class LayoutManagerExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(2, false)); shell.setText("LayoutManager Example"); shell.setSize(400, 300); Label nameLabel = new Label(shell, SWT.NONE); nameLabel.setText("Name:"); Text nameText = new Text(shell, SWT.BORDER); nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); Label ageLabel = new Label(shell, SWT.NONE); ageLabel.setText("Age:"); Text ageText = new Text(shell, SWT.BORDER); ageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); Button okButton = new Button(shell, SWT.PUSH); okButton.setText("OK"); okButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); Button cancelButton = new Button(shell, SWT.PUSH); cancelButton.setText("Cancel"); cancelButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } ``` 在这个示例中,我们创建了一个 SWT 的 Shell,并设置了其布局管理器为 GridLayout。GridLayout 接受两个参数,第一个参数表示行数,第二个参数表示列数。我们将行数设置为 2,列数设置为 false,表示列宽不相等。 接着,我们创建了四个控件,分别是 Label、Text 和 Button,并分别放置在不同的位置上。我们使用 GridData 来设置控件的布局参数。GridData 接受四个参数,分别是水平方向的填充方式、垂直方向的填充方式、是否占据多行、是否占据多列。 最后,我们打开了 Shell,并启动事件循环,以便用户可以与界面进行交互。 希望这个示例可以帮助你理解如何在 SWT 中创建和设置布局管理器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撩得Android一次心动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值