如何从 Android 中的自定义选项卡布局反转视图绑定?

问题描述

我为我的选项卡布局创建了一个自定义选项卡项,并使用视图绑定对其进行初始化,如下所示:

val tabView = CustomTabBinding.inflate(LayoutInflater.from(mContext),null,false)
        tabView.tvCustomTabTitle.text = it.title
        tabView.tvCustomTabCount.visibility = View.GONE

现在,当用户选择/取消选择选项卡时,我想更改此自定义视图的外观。通常我使用 kotlin 合成来实现这一点,如下所示:

fun setonSelectView(tabLayout: TabLayout,position: Int = 0) {
    val tab = tabLayout.getTabAt(position)
    val selected = tab?.customView
    if (selected != null)
        selected.tv_custom_tab_title?.apply {
            setTextColor(mContext.getColorCompat(R.color.colorAccent))
            typeface = setFont(true)
        }
    selected?.tv_custom_tab_count?.apply {
        setBackgroundResource(R.drawable.bullet_accent)
        mContext.getColorCompat(android.R.color.white)
    }
}

但是现在我如何使用视图绑定来实现这一点?

我使用的是 findViewById() 的方法

fun Context.setonSelectView(tabLayout: TabLayout,position: Int = 0) {
val tab = tabLayout.getTabAt(position)
val selected = tab?.customView
if (selected != null){
    val title = selected.findViewById<TextView>(R.id.tv_custom_tab_title)
    val count = selected.findViewById<TextView>(R.id.tv_custom_tab_count)
    title.apply {
        setTextColor(getColorCompat(R.color.colorAccent))
        typeface = setFont(true)
    }
    count.apply {
        setBackgroundResource(R.drawable.bullet_accent)
        getColorCompat(android.R.color.white)  
    }
}
}

但我希望有更好的方法来做到这一点。如果是,那么请帮帮我。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)