【问题标题】:Bottom item cut in reyclerview in android在android的recyclerview中切割底部项目
【发布时间】:2022-06-12 05:54:26
【问题描述】:

嘿,我正在使用 recylerview 进行约束布局。我的底部项目在屏幕上被剪切。我读到了这个堆栈溢出post。我不想使用相对布局或线性布局。有人可以指导我如何在约束布局中解决这个问题。

abc.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="16dp"
        app:closeIcon="@drawable/ic_cancel"
        app:layout_constraintBottom_toTopOf="@+id/exploreScroll"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_bias="0.0" />

    <HorizontalScrollView
        android:id="@+id/exploreScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@+id/exploreList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchView">

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/exploreChips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipSpacingHorizontal="10dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:singleLine="true"
            app:singleSelection="true" />

    </HorizontalScrollView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/exploreList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:paddingTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/exploreScroll" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的观点从

更新

@Zain 根据你的建议,我在我的 xml 中尝试了任何我的 HorizontalScrollView 落后于我的 RV。我正在添加我的蓝图,您可以清楚地看到,HorizontalScrollView 落后了。

【问题讨论】:

    标签: android kotlin android-layout android-recyclerview android-constraintlayout


    【解决方案1】:

    免责声明 使用wrap_content 高度和垂直RecyclerView 可以对回收视图方面的性能产生影响;特别 如果高度要经常变化。检查this article 更多说明。

    所以,第一步是指定RecyclerView 高度或对其进行约束;从您希望它扩展到底部的约束;所以使用0dp。但是为了使包裹RecyclerView的内容的最小高度(以防项目不超过屏幕高度);你可以使用app:layout_constraintHeight_min="wrap" 约束。

    然后从HorizontalScrollView 中删除app:layout_constraintBottom_toTopOf="@+id/exploreList",这实际上使RV 的底部项目隐藏(您的主要问题);因为它是一个过度约束; HorizontalScrollView 倾向于将RV 推到底部,而RV 倾向于将HorizontalScrollView 推到顶部。

    将其添加到布局中:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.appcompat.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="16dp"
            app:closeIcon="@drawable/ic_cancel"
            app:layout_constraintBottom_toTopOf="@+id/exploreScroll"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintVertical_chainStyle="packed" />
    
        <HorizontalScrollView
            android:id="@+id/exploreScroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:scrollbars="none"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/searchView">
    
            <com.google.android.material.chip.ChipGroup
                android:id="@+id/exploreChips"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipSpacingHorizontal="10dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:singleLine="true"
                app:singleSelection="true" />
    
        </HorizontalScrollView>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/exploreList"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="20dp"
            android:paddingTop="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_min="wrap"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/exploreScroll" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 嘿@Zain 我尝试了您的解决方案,但我的代码出现问题。请检查我的问题中的UPDATE 部分。谢谢。
    猜你喜欢
    • 2018-05-12
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2020-11-04
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多