Android RecyclerView不是';不显示所有没有滚动的项目

Android RecyclerView不是';不显示所有没有滚动的项目,android,gridview,android-recyclerview,scroll,Android,Gridview,Android Recyclerview,Scroll,我有水平的RecyclerView,它有GridView。RecyclerView有31个元素,但有30个元素可见。我想显示所有recyclerView项目,但如果不向下滚动,所有元素都不可见 当recyclerView高度为200dp时,所有项目都可见,但在包装内容时不显示。我不想给出一个固定的高度,因为它在每个手机上看起来都不一样 RecyclerView代码: RecyclerView recyclerView = findViewById(R.id.recyclerView); Line

我有水平的RecyclerView,它有GridView。RecyclerView有31个元素,但有30个元素可见。我想显示所有recyclerView项目,但如果不向下滚动,所有元素都不可见

当recyclerView高度为200dp时,所有项目都可见,但在包装内容时不显示。我不想给出一个固定的高度,因为它在每个手机上看起来都不一样

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);
<?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>
<?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 recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(linearLayoutManager);
new PagerSnapHelper().attachToRecyclerView(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>
<?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 recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(linearLayoutManager);
new PagerSnapHelper().attachToRecyclerView(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>
<?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仍在执行嵌套滚动。 您还必须禁用嵌套滚动

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

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

  • 回收器视图项应为实际的
    gridViewLayoutItem
  • 摆脱你的
    gridView
    (现在就回收服务项目布局)
  • 使用
    GridLayoutManager
    作为“recyclerView”的布局管理器
  • recyclerView.setLayoutManager(新的GridLayoutManager(context,numberOfColumns));
    
  • 您可以通过扩展功能根据屏幕宽度决定
    numberOfColumn
    :较大屏幕的列数越多,反之亦然

  • 您正在使用哪个布局管理器进行recyclerView?LinearLayoutManager。我加上我的密码谢谢。我的recyclerView对所有页面都有GridView。它是否涵盖了所有这些?您的回收器视图项目应该是直接布局,而不是网格视图。这对性能有害,而且不可扩展。