【问题标题】:set recyclerView item height wrap_content设置 recyclerView 项目高度 wrap_content
【发布时间】:2019-07-25 23:45:15
【问题描述】:

我正在从 firebase 将图像加载到 recyclerview item(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>

row.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

然后我上下滚动,查看高度。 Scrolled up/down output

第三次, again scrolled and worst output

请帮帮我..非常感谢..

【问题讨论】:

  • 您是否每次都在正确的位置看到正确的图像,即使您快速上下滚动?不仅仅是图片大小
  • @DominikWuttke 是的,我看到了......只有滚动时它们的大小会发生变化......我只希望图像视图根据我加载的图像缩放其高度。谢谢

标签: java android xml android-recyclerview


【解决方案1】:

在 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 我们保持图像的纵横比同时保持宽度与父级匹配但让高度相应地改变,因此我们仍然能够控制图像的大小(有点) 并同时保持纵横比。

您还可以在回收器视图中设置交错网格布局管理器以具有不同的项目大小。

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

【讨论】:

    【解决方案2】:

    试试下面的代码

            <?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 并以父对象为中心),您可以通过在比率前面,用逗号分隔。

    【讨论】:

      【解决方案3】:
      • 如果您使用约束布局,请使用约束尺寸比率作为 上面提到过。

        <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。 p>

      还有另一种解决方法可以最大限度地减少这种延迟。

      您可以在运行时在 onBindViewHolder 中获取 imageView 的宽度,并将高度设置为宽度或宽度的任意倍数以达到您想要的比例。

       CoroutineScope(Dispatchers.IO).launch {
            val width = holder.image.width
      
            withContext(Dispatchers.Main){
                 holder.image.layoutParams.height = width
            }
        }
      

      在后台线程获取宽度将减轻主线程的负载,最终导致平滑滚动。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多