Android 带有 RecyclerView 的 ScrollView 比我预期的要短

Android ScrollView with RecyclerView is shorter than I expected

我在ScrollView里面有两个RecyclerView。第二个RecyclerView名为“recyclerView_main_article”无法完全显示。我的意思是它的底部被切掉了。我试图在 RecyclerView 下添加名为 "empty" 的额外视图来制作一些 space。添加"empty"后,“recyclerView_main_article”显示完整,但"empty"比它的高度短。 这里是 XML.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <android.support.constraint.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/view_title_line"
            android:layout_width="5dp"
            android:layout_height="24dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:background="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_top_movie_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/top_movie_title"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line"
            app:layout_constraintStart_toEndOf="@+id/view_title_line" />

        <TextView
            android:id="@+id/textView_see_all_movies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/see_all"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line"
            app:layout_constraintEnd_toEndOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_main_video"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view_title_line" />

        <View
            android:id="@+id/view_title_line2"
            android:layout_width="5dp"
            android:layout_height="24dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:background="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/recyclerView_main_video" />

        <TextView
            android:id="@+id/textView_top_article_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/top_article_title"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line2"
            app:layout_constraintStart_toEndOf="@+id/view_title_line2" />

        <TextView
            android:id="@+id/textView_see_all_articles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/see_all"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line2"
            app:layout_constraintEnd_toEndOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_main_article"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view_title_line2" />

        <!--<View
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/holo_orange_dark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/recyclerView_main_article" />-->
    </android.support.constraint.ConstraintLayout>
</ScrollView>

我通过这个 Kotlin 代码将 HORIZONTAL 应用到 RecyclerViews。

recyclerView_main_video.layoutManager = LinearLayoutManager(this, OrientationHelper.HORIZONTAL, false)
recyclerView_main_article.layoutManager = LinearLayoutManager(this, OrientationHelper.HORIZONTAL, false)

我使用 Android Studio 3.0.1,我的 recyclerview 版本是 26.1.0。

implementation 'com.android.support:recyclerview-v7:26.1.0'

我该怎么办??我需要你的帮助。

尝试使用 NestedScrollView 而不是 ScrollView

ScrollView 中使用 RecyclerView 是个坏主意,最佳做法是使用 recyclerview 并将要滚动的所有内容放入其中。如果您的回收物品上方有一些视图,只需使用自己的 viewHolder 制作一个 header 并将其放在您的 recyclerView

的第一个位置

Change ScrollView to NestedScrollView

Add android:paddingBottom="50dp" android:clipToPadding="false" in second recyclerview

对于

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <android.support.constraint.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/view_title_line"
            android:layout_width="5dp"
            android:layout_height="24dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:background="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_top_movie_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/top_movie_title"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line"
            app:layout_constraintStart_toEndOf="@+id/view_title_line" />

        <TextView
            android:id="@+id/textView_see_all_movies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/see_all"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line"
            app:layout_constraintEnd_toEndOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_main_video"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view_title_line" />

        <View
            android:id="@+id/view_title_line2"
            android:layout_width="5dp"
            android:layout_height="24dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:background="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/recyclerView_main_video" />

        <TextView
            android:id="@+id/textView_top_article_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/top_article_title"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line2"
            app:layout_constraintStart_toEndOf="@+id/view_title_line2" />

        <TextView
            android:id="@+id/textView_see_all_articles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/see_all"
            app:layout_constraintBottom_toBottomOf="@+id/view_title_line2"
            app:layout_constraintEnd_toEndOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_main_article"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:paddingBottom="50dp"
            android:clipToPadding="false"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view_title_line2" />

        <!--<View
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/holo_orange_dark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/recyclerView_main_article" />-->
    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>