每个水平 RecyclerViews 项目的不同宽度

时间:2021-02-12 11:04:27

标签: android kotlin android-recyclerview

我的水平 RecyclerView 出现问题。我需要创建每个项目的不同宽度的 RecyclerView。我为此使用 wrap_content

<?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

但是当我滚动 recyclerview 并到达第一个元素时,我得到了这个:

enter image description here

我认为这是因为适配器每次都会重绘项目。这是我的适配器的代码:

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
        }
    }
}

我还有一个问题。如何根据项目高度动态设置 cardCornerRadius ?

提前致谢!

1 个答案:

答案 0 :(得分:1)

卡片内的 TextView 是问题所在。

文本视图为 match_parent

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

但是父对象是 wrap_content

TextView 更改为 wrap_content,然后每个单词都将是 TextView 的大小,而 CardView 将具有孩子的大小。