防止按钮在选中时变得透明

Prevent button from becoming transparent when selected

我有一个 recyclerview,上面有一排按钮。我的 objective 是让当前选中的按钮的文本为蓝绿色,而其余按钮的文本为灰色。

为此,我的 TabAdapter class 中有以下代码:

class TabAdapter(private val items: ArrayList<Pair<String, ArrayList<String>>>, private val context: Context) : RecyclerView.Adapter<TabViewHolder>() {
private var selectedPosition: Int = RecyclerView.NO_POSITION

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
    return TabViewHolder(LayoutInflater.from(context).inflate(R.layout.tabrecycler_item_column, parent, false))
}

override fun getItemCount(): Int {
    return items.size
}

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

    context as AnimeFaceKeyboard

    holder.itemView.isSelected = selectedPosition == position

    if (holder.itemView.isSelected) {
        holder.button.setTextColor(ContextCompat.getColor(context, R.color.material_deep_teal_200))
    } else {
        holder.button.setTextColor(ContextCompat.getColor(context, R.color.material_grey_600))
    }


    //This code sets the widths of the buttons to, at minimum,
    //occupy the entire width of the screen combined 
    val displayMetrics = Resources.getSystem().displayMetrics

    if (itemCount * (120 * displayMetrics.density) < displayMetrics.widthPixels) {
        holder.button.width = displayMetrics.widthPixels / itemCount
    }


    holder.button.text = items[position].first
    holder.button.setOnClickListener {

        //TODO - Figure out how this code works
        notifyItemChanged(selectedPosition)
        selectedPosition = holder.layoutPosition
        notifyItemChanged(selectedPosition)

        //This code updates a different recyclerview. 
        context.isFavoritesTabSelected = position == items.lastIndex
        context.updateCurrentImageLayout(items[position].second)
    }
}

}
class TabViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val button: Button = view.tabButton

}

相关行在onBindViewHolder方法中。特别是这一行

holder.itemView.isSelected = selectedPosition == position

此代码位于按钮的 onClick 方法中

        notifyItemChanged(selectedPosition)
        selectedPosition = holder.layoutPosition
        notifyItemChanged(selectedPosition)

这里还有 recyclerview 中按钮的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/tabButton"
        android:layout_width="match_parent"
        android:minWidth="120dp"
        android:layout_height="match_parent"
        android:text="Unset-Button-Text"
        android:background="@color/darkshade"
        style="@style/Widget.AppCompat.Button.Borderless"/>


</android.support.constraint.ConstraintLayout>

我的 recyclerView 的行为部分起作用。当前选定按钮的文本实际上是蓝绿色的。

但是,有两个问题

1] 按钮在被点击时会变成部分透明。点击时的波纹动画肯定有问题

2] 波纹动画应该只为当前选中的按钮播放,但是,它也会为之前选中的按钮播放。

这是我 phone 的 GIF 演示:

根据您的代码,这些行导致了问题

    notifyItemChanged(selectedPosition)
    selectedPosition = holder.layoutPosition
    notifyItemChanged(selectedPosition)

之前选择的按钮正在闪烁,因为您在其上调用 notifyItemChanged() 以取消选择它并且适配器从头开始重新创建它以更新它。 然后,当前选定的按钮发生了同样的事情,从头开始重新创建以更新 UI 上的更改。

您可以尝试实现 TabLayout,因为如果您使用选项卡而不是 RecylerView,这样的布局效果最好。

阅读@Shashwat 的回答后,我通过将 recyclerview 的背景颜色更改为与按钮相同的颜色来解决问题。

这意味着即使适配器从头开始重新创建按钮,也不会出现淡入淡出。