RecyclerView 不显示没有滚动的所有项目
RecyclerView isn't show all items without scroll
我有水平的 RecyclerView,它有 GridView。 RecyclerView 有 31 个元素,但有 30 个元素是可见的。我想显示所有 recyclerView 项目,但如果不向下滚动则所有元素都不可见。
当 recyclerView 高度为 200dp 时,所有项目都可见,但在 wrap_content 时不显示。我不想给出一个固定的高度,因为它在每个 phone.
上看起来都不一样
RecyclerView代码:
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>
回收站查看所有项目:
可见项目:
滚动后:
实际上,即使您已将其高度设置为 wrap_content,RecyclerView 仍在执行嵌套滚动。
您还必须禁用嵌套滚动。
RecyclerView recycler = (RecyclerView) findViewById(R.id.recyclerView);
recycler.setNestedScrollingEnabled(false);
为了实现这一点,您必须在回收站视图中进行一些更改:
- Recycler 查看项目应该是您的实际
gridViewLayoutItem
- 摆脱你的
gridView
(现在是recyclerViewItemLayout)
- 使用
GridLayoutManager
作为 `recyclerView 的布局管理器
recyclerView.setLayoutManager(new GridLayoutManager(context, numberOfColumns));
- 您可以通过扩展功能根据屏幕宽度决定
numberOfColumn
:屏幕越大,列数越多,反之亦然
我有水平的 RecyclerView,它有 GridView。 RecyclerView 有 31 个元素,但有 30 个元素是可见的。我想显示所有 recyclerView 项目,但如果不向下滚动则所有元素都不可见。
当 recyclerView 高度为 200dp 时,所有项目都可见,但在 wrap_content 时不显示。我不想给出一个固定的高度,因为它在每个 phone.
上看起来都不一样RecyclerView代码:
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>
回收站查看所有项目:
可见项目:
滚动后:
实际上,即使您已将其高度设置为 wrap_content,RecyclerView 仍在执行嵌套滚动。 您还必须禁用嵌套滚动。
RecyclerView recycler = (RecyclerView) findViewById(R.id.recyclerView);
recycler.setNestedScrollingEnabled(false);
为了实现这一点,您必须在回收站视图中进行一些更改:
- Recycler 查看项目应该是您的实际
gridViewLayoutItem
- 摆脱你的
gridView
(现在是recyclerViewItemLayout) - 使用
GridLayoutManager
作为 `recyclerView 的布局管理器
recyclerView.setLayoutManager(new GridLayoutManager(context, numberOfColumns));
- 您可以通过扩展功能根据屏幕宽度决定
numberOfColumn
:屏幕越大,列数越多,反之亦然