kotlin中介绍PopupWindow中textView?.isSelected = false的不变色bug

来源:互联网 发布:input失去焦点触发js 编辑:程序博客网 时间:2024/06/07 03:45

在kotlin中的写法和java中基本一样 因为经常用到就直接粘在这里了

  private fun showSellerPop() {        if (mPopupWindow == null)            mPopupWindow = PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply {                val inflate = View.inflate(activity, R.layout.layout_popwin_supplier_list, null)                setBackgroundDrawable(ColorDrawable())                contentView = inflate                val recyclerView = inflate.find<RecyclerView>(R.id.popwin_list_view)                recyclerView?.layoutManager = GridLayoutManager(activity, 4, GridLayoutManager.VERTICAL, false)                sellerAdapter = object : CommonRecycleViewAdapter<BussinessTypeData>(activity, R.layout.item_popwin_list, mMenuData1) {                    override fun convert(helper: ViewHolderHelper?, good: BussinessTypeData, position: Int) {                        val textView = helper?.getView<TextView>(R.id.item_popwin_tv)                        val class_name = good.class_name                        val class_id = good.class_id                        textView?.text = class_name                        textView?.isSelected = false                        if (class_name == titleBarselectimg?.text.toString()) {                            //textView?.setBackgroundResource(R.drawable.item_popwin_select_bg)                            //textView?.setTextColor(Color.parseColor("#ffffffff"))                            textView?.isSelected = true                        }                        textView?.onClick {                            searchBean.class_id = class_id                            titleBarselectimg?.text = class_name                            dismiss()                            toSearch()                            notifyDataSetChanged()                        }                    }                }                recyclerView?.adapter = sellerAdapter                isFocusable = true                isOutsideTouchable = true            }        mPopupWindow?.showAsDropDown(line, 0, 0)    }

其实我最想说是convert部分,说也说不明白直接看下图
这种点击状态选择器相信大家都可以随便写了吧,不过就是这个简单的状态选在器却困扰了我很久,以前的项目中都是用的CheckBox替代TextView的。
这是TextView的标准选择器写法

看清楚顺序从上到下 , 下面两个默认状态一定写最下面

<item android:color="@color/rgb_85_85_85"></item><item android:drawable="@drawable/item_popwin_bg"></item>
<TextView        android:id="@+id/item_popwin_tv"        android:layout_width="@dimen/dimen_80dp"        android:layout_height="@dimen/dimen_30dp"        android:background="@drawable/item_popwin_selector"        android:gravity="center"        android:singleLine="true"        android:text="酒店"        android:textColor="@color/item_grid_selector"        android:textSize="@dimen/textSize_14" />

item_popwin_selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/item_popwin_select_bg" android:state_selected="true"></item>    <item android:drawable="@drawable/item_popwin_select_bg" android:state_pressed="true"></item>    <item android:drawable="@drawable/item_popwin_bg"></item></selector>

item_grid_selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/white" android:state_pressed="true"></item>    <item android:color="@color/white" android:state_selected="true"></item>    <item android:color="@color/rgb_85_85_85"></item></selector>

这里写图片描述

                    override fun convert(helper: ViewHolderHelper?, good: BussinessTypeData, position: Int) {                        val textView = helper?.getView<TextView>(R.id.item_popwin_tv)                        val class_name = good.class_name                        val class_id = good.class_id                        textView?.text = class_name                        textView?.isSelected = false                        if (class_name == titleBarselectimg?.text.toString()) {//textView?.setBackgroundResource(R.drawable.item_popwin_select_bg)//textView?.setTextColor(Color.parseColor("#ffffffff"))                            textView?.isSelected = true                        }                        textView?.onClick {                            searchBean.class_id = class_id                            titleBarselectimg?.text = class_name                            dismiss()                            toSearch()                            notifyDataSetChanged()                        }                    }

看客老爷可以说我说的话啰嗦是废话,可是恰恰就是最细微的bug才难以发现,慢慢积累成为不得不面对的问题。所以在此mark一下,提醒自己,也希望正在为此困惑的童鞋可以发现并解决这个问题。

sellerAdapter 是另外一个刷新库
我还写了一个PopWindow工具类下次加进来

阅读全文
0 0
原创粉丝点击