Alon Shlider
Alon Shlider

Reputation: 1298

Very long image being cut when the height of the image is not enough

Here are the 2 situations I have in my app -

enter image description here

enter image description here

as you can see, the image is being centered when it is being enlarged.

The wanted result is that I would like to have see the left side of the image always, meaning let's say the 40% left side of the image.

Here is my holder xml -

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

    <androidx.cardview.widget.CardView
        android:id="@+id/search_image_contact_cardview"
        android:layout_width="152dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        app:cardCornerRadius="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/vendors_row_item_imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            tools:src="@mipmap/ic_launcher" />


    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

here is my holder class -

class VendorsHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var vendorImageView: ImageView = itemView.findViewById(R.id.vendors_row_item_imageview)
    var rootLayout: ConstraintLayout = itemView.findViewById(R.id.vendors_row_item_root_layout)
    var vendorHolderCardview: CardView = itemView.findViewById(R.id.search_image_contact_cardview)

}

adapter -

class VendorAdapter(private val miniVendorModels: List<MiniVendorModel>, private val context: Context) : RecyclerView.Adapter<VendorsHolder>() {

    companion object {
        const val EXTRA_VENDOR_MODEL = "EVM"
    }

    private val vendorsHoldersList = mutableListOf<VendorsHolder>()

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): VendorsHolder {
        val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.fragment_marketplace_vendor_row_item, viewGroup, false)
        val vendorsHolder = VendorsHolder(view)
        vendorsHoldersList.add(vendorsHolder)
        return vendorsHolder
    }

    override fun onBindViewHolder(vendorsHolder: VendorsHolder, i: Int) {
        val model = miniVendorModels[i]
        Picasso.get().load(model.bannerPicture).into(vendorsHolder.vendorImageView)
        vendorsHolder.vendorImageView.setOnClickListener { v: View? ->
            try {
                val intent = Intent(context, VendorPageActivity::class.java)
                intent.putExtra(EXTRA_VENDOR_MODEL, model)
                context.startActivity(intent)
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(context, ResourceHelper.getString(R.string.marketplace_vendor_unavailable), Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun getItemCount(): Int = miniVendorModels.size

    fun resizeAllHolders(height : Int){
        vendorsHoldersList.forEach { holder ->
            holder.rootLayout.updateLayoutParams {
                this.height = height
            }
        }
    }


}

any ideas of how to implement this one?

Upvotes: 0

Views: 26

Answers (1)

Lena Bru
Lena Bru

Reputation: 13947

try changing

   <ImageView
            android:id="@+id/vendors_row_item_imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            tools:src="@mipmap/ic_launcher" />

to

<ImageView
            android:id="@+id/vendors_row_item_imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            tools:src="@mipmap/ic_launcher" />

Upvotes: 0

Related Questions