1 year ago

#280697

test-img

james04

ImageView inside recyclerView proper dimensions

I am developing a chat Android application. Inside the chatRecyclerView i want to show Images along with all other views. However, since i dont know the size of each image i cannot pre-define inside the xml the width-height of the img. If i define those with wrap_content, wrap_content, this makes the recycler scroll without smoothness and also the image to show very big or very small.

I came up with a solution like the below.

       <ImageView
            android:id="@+id/img_rcv_msg_image_landscape"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="32dp"
            android:adjustViewBounds="true"
            android:maxWidth="320dp"
            android:maxHeight="320dp"
            android:minWidth="150dp"
            android:minHeight="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/error_image"
            app:layout_constraintBottom_toBottomOf="@+id/img_frame"
            app:layout_constraintEnd_toEndOf="@+id/img_frame"
            app:layout_constraintStart_toStartOf="@+id/img_frame"
            app:layout_constraintTop_toBottomOf="@+id/img_rcvName"
            tools:ignore="ContentDescription" />

       <ImageView
            android:id="@+id/img_rcv_msg_image_portrait"
            android:layout_width="wrap_content"
            android:layout_height="280dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="32dp"
            android:adjustViewBounds="true"
            android:maxWidth="320dp"
            android:maxHeight="320dp"
            android:minWidth="150dp"
            android:minHeight="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/error_image"
            app:layout_constraintBottom_toBottomOf="@+id/img_frame"
            app:layout_constraintEnd_toEndOf="@+id/img_frame"
            app:layout_constraintStart_toStartOf="@+id/img_frame"
            app:layout_constraintTop_toBottomOf="@+id/img_rcvName"
            tools:ignore="ContentDescription" />

I have 2 imageViews. One for the LandScape and one for the Portrait mode. The only data that i have in the ViewHolder is the File of the Image and i dont know which one is the proper image to show.

val file = messageItem.messageItemToFile(itemView.context, it) ?: return@let

//Hide both
imageViewLand.visibility = View.GONE
imageViewPortrait.visibility = View.GONE

Glide
     .with(itemView.context)
     .load(file)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .into(
        if (file_width > file_height) imageViewLand else imgeViewPortrait)
     )

Is there a better solution to come up with??

android

kotlin

android-recyclerview

imageview

android-bitmap

0 Answers

Your Answer

Accepted video resources