Android RecyclerView 大间隙 space 在平板电脑景观上的一些项目行之后

Android RecyclerView large gap space after some rows of items on tablet landscape

我的 RecyclerView 中的视图行之间出现了很大的随机放置的间隙,就像其他用户在下面的问题中提到的一样。

我研究了其他问题的建议解决方案,例如:

Android RecyclerView Blank Space

RECYCLERVIEW SPACE BETWEEN ITEMS ON ANDROID APPLICATION

但它们都在暗示同一件事:将布局中的 match_parent 更改为 wrap_content。从下面的布局代码可以看出,我已经尝试过了,但仍然有同样的差距。

我需要注意,这些差距只存在于风景中的平板电脑中。

我的布局:

content_main.xml:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

content_main.xml 在 layout-sw600pd-port:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/llTwoPane"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:baselineAligned="true"
              android:divider="?android:attr/dividerHorizontal"
              android:orientation="horizontal"
              android:paddingTop="@dimen/gap_xl"
              tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment_main"
        android:name=".MainFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="@integer/parts_fd"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</LinearLayout>

content_main.xml 在 layout-sw600pd

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/llTwoPane"
              android:layout_width="match_parent"
            android:layout_height="wrap_content"
              android:baselineAligned="true"
              android:orientation="horizontal"
              android:paddingTop="@dimen/gap_xl">

    <fragment
        android:id="@+id/fragment_main"
        android:name=".MainFragment"
        android:layout_width="0dp"
        android:layout_weight="@integer/parts_fm"
      android:layout_height="wrap_content"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="@integer/parts_fd"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</LinearLayout>

fragment_main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llFragmentMain"
    android:layout_width="match_parent"
  android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainFragment"
    tools:showIn="@layout/activity_main"
    android:background="@color/colorGray"
    >

    <Spinner
        android:id="@+id/spnSort"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:paddingTop="@dimen/title_gap"
        android:paddingBottom="@dimen/title_gap"
        android:paddingLeft="@dimen/gap_m"
        android:paddingRight="@dimen/gap_m"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvPosters"
        android:layout_width="match_parent"
      android:layout_height="wrap_content"
        android:padding="@dimen/half_poster_spacing"
        android:background="@color/colorLight"
        android:scrollbars="vertical"
        android:animateLayoutChanges="false"
        android:scrollbarThumbHorizontal="@drawable/scrollbar"
        />

</LinearLayout>

item.xml 在适配器中充气:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flPosterItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/half_poster_spacing"
    android:descendantFocusability="blocksDescendants"
    >
    <ImageView
        android:id="@+id/imvPoster"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/poster_content_description"
        />
    <Button
        android:id="@+id/btnFavouriteInMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="@dimen/fav_button_minw"
        android:paddingBottom="@dimen/fav_button_padding"
        android:paddingLeft="@dimen/fav_button_padding"
        android:paddingStart="@dimen/fav_button_padding"
        android:paddingRight="@dimen/fav_button_padding"
        android:paddingEnd="@dimen/fav_button_padding"
        android:background="@color/colorButtonStarBgd"
        android:textStyle="bold"
        android:textAlignment="center"
        android:textSize="@dimen/text_size_m"
        android:layout_gravity="bottom|end"
        />
</FrameLayout>

我将 RecyclerView 设置为:

recyclerView.setLayoutManager(posterAdapter.getGridLayoutManager());
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(posterAdapter);

网格布局管理器设置为:

GridLayoutManager getGridLayoutManager() {
    GridLayoutManager gridLayoutManager = new GridLayoutManager(context, mNumberOfColumns);
    gridLayoutManager.setSmoothScrollbarEnabled(true);
    gridLayoutManager.setAutoMeasureEnabled(true);
    gridLayoutManager.setSpanCount(mNumberOfColumns);
    return gridLayoutManager;
}

在手机上,任何方向都不会出现间隙。在平板电脑上,纵向只有一列。即使 match_parent 也不会出现间隙。在横向中,有三到四列,即使 wrap_content 也会出现间隙,并且总是在整行之间。

我已经尝试删除

但是没用。

有人有其他解释吗?

如上述其他问题的答案中所述,从 match_parent 更改为 wrap_content 应该可以解决问题。但事实并非如此,如果有人能解释为什么我会接受它作为答案。

我已经通过为 RecyclerView 和相关布局高度返回 match_parent 并为 GridLayoutManager 设置 setAutoMeasureEnabled(false) 解决了这个问题(上面 getGridLayoutManager() 函数的第三行- 刚刚将 true 更改为 false).