【发布时间】:2020-09-23 03:29:36
【问题描述】:
我有具有 GridView 的水平 RecyclerView。 RecyclerView 有 31 个元素,但 30 个元素是可见的。我想显示所有 recyclerView 项目,但如果不向下滚动,所有元素都不可见。
当 recyclerView 高度为 200dp 时,所有项目都可见,但在 wrap_content 时不会出现。我不想给出一个固定的高度,因为它在每部手机上看起来都不一样。
Recycler查看代码:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(linearLayoutManager);
new PagerSnapHelper().attachToRecyclerView(recyclerView);
Recyclerview 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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" />
<TextView
android:id="@+id/infoBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_below="@+id/recyclerView"/>
</RelativeLayout>
Recyclerview 项目布局:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="7"
android:stretchMode="columnWidth" />
GridView 项目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_margin="3dp"
android:textColor="@color/white"
android:gravity="center"
android:textSize="@dimen/textSize" />
</RelativeLayout>
滚动后:
【问题讨论】:
-
您的 recyclerView 使用哪个布局管理器?
-
线性布局管理器。我添加我的代码
标签: android gridview android-recyclerview scroll