使用RecyclerView的Android ScrollView比我预期的要短

时间:2018-02-22 09:49:26

标签: android android-recyclerview kotlin android-scrollview

RecyclerView内有两个ScrollView。第二个RecyclerView命名为" recyclerView _ main_article"无法完全展示。我的意思是它的底部被切掉了。我试图添加名为&#34的额外视图;空"在RecyclerView之下留出一些空间。我添加"空"," recyclerView _ main_article"完全显示但是#34;空的"比它的高度短。 这是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'

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

3 个答案:

答案 0 :(得分:1)

RecyclerView中使用ScrollView是一个坏主意,最佳做法是使用recyclerview并将您想要的所有内容滚动到其中。如果您的回收商品上方有一些观看次数,只需使用自己的viewHolder标题并将其放在recyclerView的第一个位置

答案 1 :(得分:1)

  

ScrollView更改为NestedScrollView

     

在第二个android:paddingBottom="50dp"

中添加android:clipToPadding="false" recyclerview

clipToPadding

试试这个:

<?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>

答案 2 :(得分:0)

尝试使用NestedScrollView代替ScrollView