首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >防止按钮在选中时变得透明

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

Stack Overflow用户
提问于 2018-06-06 08:21:54
回答 2查看 72关注 0票数 0

我有一个有一排按钮的recyclerview。我的目标是使当前选中的按钮具有蓝绿色的文本,而其余的按钮具有灰色的文本。

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

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]涟漪动画应该只为当前选择的按钮播放,但也会为先前选择的按钮播放。

以下是我手机中的GIF演示:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-06 08:57:35

在阅读了@Shashwat的答案后,我解决了这个问题,将recyclerview的背景颜色更改为与按钮相同的颜色。

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

票数 0
EN

Stack Overflow用户

发布于 2018-06-06 08:32:08

从您的代码来看,这些代码行导致了问题

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

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

您可以尝试实现TabLayout,因为如果您使用tabs而不是RecylerView,像这样的布局会工作得最好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50710648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档