【问题标题】:How to ase RecyclerView Instead of ScrollView?如何使用 RecyclerView 而不是 ScrollView?
【发布时间】:2023-02-22 05:28:39
【问题描述】:

如何使用 RecyclerView 而不是 ScrollView? ScrollView 给我错误,所以我想使用 RecyclerView 而不是 ScrollView。

这是我的第一个片段布局代码:

<?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:id="@+id/homeF"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/androidbackground2"
    tools:context=".ui.home.HomeFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

当我想从这个布局移动到下面的布局时:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/androidbackground2">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

我收到这样的错误:

java.lang.IllegalStateException: ScrollView can host only one direct 孩子

由于此 ScrollView 错误,我无法切换布局。如何解决这个问题?

【问题讨论】:

    标签: java android android-recyclerview scrollview


    【解决方案1】:

    首先,您的代码会产生此错误:

    这个 androidx.constraintlayout.widget.ConstraintLayout 应该使用 android:layout_height="wrap_content"

    这是通过使用 wrap_content 而不是 match_parent 作为 ConstraintLayout 的高度来解决的。

    其次,不需要为ConstraintLayout提供约束属性。应将约束属性添加到 ConstraintLayout 的子视图中,以便它们生效。

    第三,在这里使用 ConstraintLayout 没有多大意义。尝试使用 LinearLayout 代替,如下所示:

    <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:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        
            ...
        </LinearLayout>
    
    </ScrollView>
    

    最后,使用 RecyclerView 不是应该使用而不是使用 ScrollView 的解决方案,并且不应该仅仅使用它来使用它的滚动功能。你应该在不同的情况下使用 RecyclerView,例如当你想呈现一个动态的项目列表时。有关更多信息,您应该阅读the official doc

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      相关资源
      最近更新 更多