recycler view方向
时间: 2023-10-04 07:07:35 浏览: 73
RecyclerView可以设置水平和垂直两种方向。
设置垂直方向:
```
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
```
设置水平方向:
```
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
```
也可以使用GridLayoutManager来设置方向:
```
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
layoutManager.setOrientation(GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
```
其中,第二个参数2代表每行显示2个item,如果要设置水平方向,可以将第三行改为:
```
layoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
```
阅读全文