如何在RecyclerView中处理可变高度的图像?

时间:2019-04-19 20:48:39

标签: android android-layout android-recyclerview android-imageview recycler-adapter

我正在聊天应用程序。为此,如果消息是图像类型,我想包装高度并固定宽度。但是问题在于,由于加载图像需要花费一些时间,因此聊天消息会有所变化,直到加载图像为止,因此很容易发生。我该如何处理?

<android.support.constraint.ConstraintLayout
    android:id="@+id/parent_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/send_message_box"
    android:maxWidth="228dp">

    <android.support.v7.widget.AppCompatImageView
            android:id="@+id/iv_message"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="200dp"
            android:layout_height="0dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:padding="11dp"
            android:visibility="gone" />
</android.support.constraint.ConstraintLayout>

这是我正在使用的图像布局。

2 个答案:

答案 0 :(得分:0)

您可以固定高度并显示占位符图像,直到加载图像为止。您获得了使用毕加索显示占位符图像(直到加载图像)的功能,请选中此https://square.github.io/picasso/

答案 1 :(得分:0)

在下载图像时,可以使用默认图像设置初始高度。图像下载后,如果将高度设置为wrap_content,它将占用图像的高度。因此,您需要像以下那样修改ImageView

<ImageView
    android:id="@+id/iv_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@mipmap/ic_image_loader_placeholder" />

请根据您的要求修改ImageView

您可能还会在我实现了类似想法的Github project上看到。我必须调用Flickr的API并将图像加载到RecyclerView中。希望有帮助!