簡體   English   中英

Android Recyclerview 沒有重新測量它的高度

[英]Android Recyclerview not remeasuring its height

我的應用程序有很多嵌套的回收器視圖,在其中一個視圖中,我有一個布局,其中包含一個圖像和一個像這樣的文本視圖。

在此處輸入圖像描述

文本視圖是一個指示器,當用戶打開該特定課程時,它會消失。 它會變成這樣:

在此處輸入圖像描述

由於該指示器僅出現在某些課程中,因此該指示器可能會消失 (view.gone) 並在某些情況下出現 (view.visible)。 這是保存這些課程的 recyclerview 代碼。

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_video_packs"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin_16dp"
                    android:clipToPadding="false"
                    android:paddingEnd="16dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/rv_continue_watching" />

請注意,回收站視圖是一個包裝內容。

當存在多個課程的情況時,某些課程可能不包含新視頻指示器。 如果那些沒有指示器的課程出現在屏幕上,那么 recyclerview 會包裹它的高度以僅保留課程圖像,就像這樣。

在此處輸入圖像描述

但問題是,當我們水平滾動時,一些課程可能包含指示器,這意味着 recyclerview 應該相應地增加其高度以容納指示器。 相反,recyclerview 無法更新其高度以環繞兩個視圖,從而導致此視圖不完整。

在此處輸入圖像描述

發生這種情況是因為 recyclerview 在滾動時無法更新其高度,因此使視圖減半。

我嘗試了多種方法來解決這個問題,例如將 setHasFixedSize 設置為 false 等。但似乎沒有用

這只是有時發生。

現在唯一可行的解決方案是將回收器視圖高度固定為特定高度,以便它同時包含兩個視圖。 但如果可能的話,我想要一個更具可擴展性和最佳的解決方案。

有沒有辦法讓recyclerview重新計算它的高度?

Recyclerview Item 查看代碼。

<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="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:clickable="true"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/iv_learn_item"
        android:visibility="invisible"
        android:layout_width="134dp"
        android:layout_height="200dp"
        android:adjustViewBounds="true"
        android:layout_marginStart="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:foreground="?attr/selectableItemBackground"
        />    

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/video_update_tv_guideline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintGuide_begin="195dp"
        android:orientation="horizontal"
        />

    <TextView
        android:id="@+id/video_update_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@id/iv_learn_item"
        app:layout_constraintEnd_toEndOf="@id/iv_learn_item"
        app:layout_constraintTop_toBottomOf="@+id/video_update_tv_guideline"
        android:layout_marginStart="@dimen/dimen_11dp"
        android:layout_marginEnd="@dimen/dimen_11dp"
        style="@style/content_update_pill"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

適配器代碼

                rvVideoPacks.layoutManager =
                    LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false)
                rvVideoPacks.setHasFixedSize(false)
                rvVideoPacks.itemAnimator = null
                rvVideoPacks.isNestedScrollingEnabled = true
                rvVideoPacks.adapter = videoPackAdapter

編輯:

我也曾嘗試使指示器不可見,但這只會在底部創建一個額外的空間,使其看起來很糟糕。 我希望 recyclerview 相應地調整其高度。

解決此問題的一種方法是,如果可能的話,為您的布局為主 ConstraintLayout 提供固定高度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM