【问题标题】:RecyclerView isn't show all items without scrollRecyclerView 不显示所有项目而不滚动
【发布时间】: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


【解决方案1】:

为了实现这一点,您必须在回收站视图中进行一些更改:

  1. Recycler view item 应该是你的实际gridViewLayoutItem
  2. 摆脱你的gridView(现在是recyclerViewItemLayout)
  3. 使用GridLayoutManager 作为`recyclerView 的布局管理器
recyclerView.setLayoutManager(new GridLayoutManager(context, numberOfColumns));
  1. 您可以通过扩展功能根据屏幕宽度决定numberOfColumn:较大屏幕的列数越多,反之亦然

【讨论】:

  • 谢谢。我的 recyclerView 对所有页面都有 gridViews。是否涵盖了所有这些
  • 您的回收站视图项目应该是直接布局而不是网格视图。这对性能不利,也无法扩展。
【解决方案2】:

实际上,RecyclerView 正在执行嵌套滚动,即使您已将其高度设置为 wrap_content。 您也必须禁用嵌套滚动。

RecyclerView recycler = (RecyclerView) findViewById(R.id.recyclerView);
recycler.setNestedScrollingEnabled(false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 2021-01-19
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多