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

在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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在PopupWindow调用这段Kotlin代码,并使用回调函数进行处理,您可以按照以下步骤进行操作: 首先,在Activity定义一个回调接口,用于在PopupWindow执行相应的操作。例如: ```kotlin interface PopupWindowCallback { fun onItemClick(position: Int) } ``` 然后,在Activity创建PopupWindow时,将回调接口传递给PopupWindow,并在PopupWindow内部使用该回调函数。示例代码如下: ```kotlin val popupWindow = PopupWindow(...) popupWindow.contentView = ... popupWindow.showAsDropDown(...) val callback = object : PopupWindowCallback { override fun onItemClick(position: Int) { mFileManagerItemRecycle?.let { mFileManagerListModelAdapter?.getItem(position)?.let { item -> if (mFileManagerAdapter?.getIsEdit() == true) {// 编辑模式 if (mFileManagerAdapter?.mFiles.isNullOrEmpty()) { mFileManagerAdapter?.mFiles?.add(item) } else { if (mFileManagerAdapter?.mFiles?.contains(item) == true) { mFileManagerAdapter?.mFiles?.remove(item) } else { mFileManagerAdapter?.mFiles?.add(item) } } mTabFileManagerSize?.text = "已选${mFileManagerAdapter?.mFiles?.size}个文件" mFileManagerAdapter?.notifyItemChanged(position) } else { if (item.isDirectory) {//是文件夹进入下一级 var path = item.path mFileManagerSecondAdapter?.addItem(item) mFilePath = path mFileManagerAdapter?.clearData() mFileManagerAdapter?.setData(FileUtils.listFilesInDir(path)) } } } } } } // 将回调接口传递给PopupWindow popupWindow.setCallback(callback) ``` 在PopupWindow,创建一个函数 `setCallback()` 来接收回调对象,并在需要的地方调用回调函数。例如: ```kotlin class YourPopupWindow : PopupWindow { private var callback: PopupWindowCallback? = null fun setCallback(callback: PopupWindowCallback) { this.callback = callback } // 在需要的地方调用回调函数 private fun onItemClicked(position: Int) { callback?.onItemClick(position) } } ``` 最后,在PopupWindow内部的点击监听器,调用 `onItemClicked()` 方法来触发回调操作。例如: ```kotlin val onClickListener = View.OnClickListener { view -> if (view.id == R.id.item) { // 获取position的值 val position = adapterPosition // 调用PopupWindow的回调函数 onItemClicked(position) } } // 设置点击监听器 yourItemView.setOnClickListener(onClickListener) ``` 请根据您的实际情况替换示例代码的 `YourPopupWindow`、`YourItemType` 和 `yourItemView` 等相关内容。希望这样能够帮助您解决问题!如有更多疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值