簡體   English   中英

設置 recyclerView 項目高度 wrap_content

[英]set recyclerView item height wrap_content

我正在從 firebase 將圖像加載到 recyclerview item(imageview) 中。所有圖像的尺寸都不同。

我希望我的 imageview 根據我正在加載的圖像縮放其高度。

主布局.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>


</android.support.constraint.ConstraintLayout>

行.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >


        <ImageView
            android:id="@+id/ImageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />


</LinearLayout>

在 java 我正在做。

RecyclerView.setHasFixedSize(true);
       RecyclerView.setLayoutManager(new LinearLayoutManager(mctx));

此外,當我滾動 recyclerview 時,它會更改圖像高度

output

然后我上下滾動,看看高度。 向上/向下滾動 output

第三次,再次滾動,最差 output

請幫助我..非常感謝..

在row.xml中使用的圖像視圖中,將adjustViewBounds設置為true,如下所示:

<ImageView
    android:id="@+id/ImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/img"/>

通過添加adjustViewBounds並將layout_height保留為wrap_content,我們可以保持圖像的長寬比,同時保持寬度與父對象匹配,但高度可以相應地更改,因此我們仍然能夠控制圖像的大小(種類)並保持同時顯示寬高比。

您還可以在recyler視圖中設置交錯的網格布局管理器以具有不同的項目大小。

recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, 1)); //First param to set span count and second for orientation

試試下面的代碼

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.constraint.ConstraintLayout  
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/rootLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools">


                <ImageView
                    android:id="@+id/ImageView"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintDimensionRatio="H,16:5" />


        </android.support.constraint.ConstraintLayout >

如果您設定

app:layout_constraintDimensionRatio =“ H,16:5”

這意味着首先要從其他約束條件中計算出寬度,然后再根據寬高比來調整高度。要根據另一側的尺寸來約束某一特定側面,我們可以在W或H之前附加(以逗號分隔的比例)分別限制寬度或高度。 例如,如果一個尺寸受兩個目標約束(例如,寬度為0dp並以父對象為中心),則可以通過在其中添加字母W(用於約束寬度)或H(用於約束高度)來指示應約束哪一側。比率的前面,以逗號分隔。

  • 如果您正在使用約束布局,請使用上面提到的約束尺寸比。

     <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintDimensionRatio="1:1"/>
  • 否則,如果您使用線性或相對布局。

     <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" />

兩者都可以解決您的問題,但僅當您需要填充少量數據時才推薦使用它們,不建議將這些技巧用於數千張圖像,因為這將在運行時計算並設置每個項目的高度,這意味着這將運行在主線程上,對於數千張圖片,這將導致滾動延遲,並可能導致低端設備出現ANR

還有另一種解決方法可以最大程度地減少這種滯后。

您可以在運行時在 onBindViewHolder 中獲取 imageView 的寬度,並將高度設置為等於寬度或寬度的任意倍數以獲得所需的比例。

 CoroutineScope(Dispatchers.IO).launch {
      val width = holder.image.width

      withContext(Dispatchers.Main){
           holder.image.layoutParams.height = width
      }
  }

在后台線程獲取寬度將減輕主線程的負載,最終導致平滑滾動。

暫無
暫無

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

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