Lemonyara
Lemonyara

Reputation: 53

Different width of each horizontal RecyclerViews items

I get a problem with my horizontal RecyclerView. I need to create RecyclerView with different width of each item. I use wrap_content for this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    android:id="@+id/recyclerItemLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardPreventCornerOverlap="false"
    app:cardCornerRadius="@dimen/_10sdp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    app:cardElevation="0dp"
    app:cardBackgroundColor="@android:color/darker_gray"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">

    <TextView
        android:id="@+id/recyclerItemText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="@dimen/_17sdp"
        android:text="test"
        android:textColor="@color/colorKeyboardRecyclerViewItemText"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="7dp"
        android:layout_marginBottom="7dp"/>

</androidx.cardview.widget.CardView>

enter image description here

But when I scroll recyclerview and get to the first element I got this:

enter image description here

I think it is because the adapter redraws items every time. Here is code of my adapter:

class KeyboardAdapter(private val fontsNames: FontsData, private val fontButtonCallback: (Int) -> Unit): RecyclerView.Adapter<KeyboardAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val v = LayoutInflater.from(parent.context).inflate(R.layout.keyboard_recyclerview_item, parent, false)

        return ViewHolder(v)
    }

    override fun getItemCount(): Int {

        return fontsNames.fontsArrayEn.count()
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.itemText.text = fontsNames.fontsArrayEn[position].name

        val selectedColor = holder.itemLayout.context.resources.getColor(R.color.colorKeyboardRecyclerViewSelectedItem)
        val backgroundColor = holder.itemLayout.context.resources.getColor(android.R.color.transparent)

        holder.itemLayout.setOnClickListener{

            fontButtonCallback(position)
            changeIsSelectedState(position)
            fontsNames.fontsArrayEn[position].isSelected = true
            notifyDataSetChanged()
        }

     /*   holder.itemLayout.post{ val itemHeight = holder.itemLayout.height.toFloat()
                                val itemRadius = itemHeight /2.5
                                holder.itemLayout.radius = Utils().intToDp(holder.itemLayout.context, itemRadius.toFloat())} */

        if (fontsNames.fontsArrayEn[position].isSelected)
            holder.itemLayout.setCardBackgroundColor(selectedColor)
        else
            holder.itemLayout.setCardBackgroundColor(backgroundColor)
    }

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){

        val itemLayout = itemView.findViewById<CardView>(R.id.recyclerItemLayout)!!
        val itemText = itemView.findViewById<TextView>(R.id.recyclerItemText)!!
    }

    private fun changeIsSelectedState(position: Int){

        for (i in 0 until fontsNames.fontsArrayEn.count()){

            fontsNames.fontsArrayEn[i].isSelected = i == position
        }
    }
}

And i have one more question. How I can set cardCornerRadius dynamically depends of item height ?

Thanks in advance!

Upvotes: 0

Views: 352

Answers (1)

cutiko
cutiko

Reputation: 10517

The TextView inside the card is the problem.

The text view is match_parent

android:layout_width="match_parent"
android:layout_height="match_parent"

But the parent is wrap_content.

Change the TextView to wrap_content and then every word is gonna be the size of the TextView and the CardView will have the size of the child.

Upvotes: 1

Related Questions