recycler view显示滚动条
时间: 2023-10-04 17:10:51 浏览: 149
要在 RecyclerView 中添加滚动条,可以使用 Android 的内置滚动条组件。以下是一些步骤:
1. 在 RecyclerView 的 XML 文件中添加 ScrollView 组件,并将 RecyclerView 包含在其中:
```
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
```
2. 在 RecyclerView 的 Java 文件中,使用 LinearLayoutManager 和 setHasFixedSize 方法来配置 RecyclerView:
```
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
```
3. 在 RecyclerView 的 XML 文件中,添加 android:scrollbars 属性来指定滚动条的类型(例如,vertical 表示垂直滚动条):
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
```
这些步骤将在 RecyclerView 中添加一个垂直滚动条。您可以根据需要更改属性值,例如添加水平滚动条等。
阅读全文
相关推荐


















