angcyo / DslTabLayout

:hearts: Android界最万能的TabLayout(不仅仅是TabLayout), 支持任意类型的item, 支持Drawable类型的指示器,智能开启滚动,支持横竖向布局等
https://github.com/angcyo/DslAdapter
MIT License
1.57k stars 148 forks source link

动态改变tab的选中颜色 #8

Closed YumikoAzu closed 4 years ago

YumikoAzu commented 4 years ago

app:tab_deselect_color="@color/text_gray" app:tab_select_color="@color/TextColorBlack" 这两个xml属性在Java代码是哪个方法可以设置?

angcyo commented 4 years ago

com.angcyo.tablayout.DslTabLayout#getTabLayoutConfig

对象DslTabLayoutConfig 中:

com.angcyo.tablayout.DslTabLayoutConfig#getTabSelectColor

com.angcyo.tablayout.DslTabLayoutConfig#getTabDeselectColor

触发重绘, 即可生效.

YumikoAzu commented 4 years ago

com.angcyo.tablayout.DslTabLayout#getTabLayoutConfig

对象DslTabLayoutConfig 中:

com.angcyo.tablayout.DslTabLayoutConfig#getTabSelectColor

com.angcyo.tablayout.DslTabLayoutConfig#getTabDeselectColor

触发重绘, 即可生效.

能重设所有选中和未选中的tab的字颜色吗?我试了下好像不行。 dslTabLayout.configTabLayoutConfig { //选中view的回调 onSelectViewChange = { fromView, selectViewList, reselect -> val toView = selectViewList.first()

}
//选中index的回调
onSelectIndexChange = { fromIndex, selectIndexList, reselect ->
    val toIndex = selectIndexList.first()

if (tabIndex == 0) { tl_news_article.tabLayoutConfig!!.tabSelectColor = activity!!.getColorValue(R.color.white) tl_news_article.tabLayoutConfig!!.tabDeselectColor = activity!!.getColorValue(R.color.white) } else { tl_news_article.tabLayoutConfig!!.tabSelectColor = activity!!.getColorValue(R.color.TextColorBlack) tl_news_article.tabLayoutConfig!!.tabDeselectColor = ContextCompat.getColor(activity!!, R.color.text_gray) } tl_news_article.invalidate() } } 我这边要这样的效果(有11个tabItem)第一个item时选中文字颜色和未选中都是白色(tablayout背景为透明)其他的第二个到第十一个选中文字颜色为黑色未选中文字颜色为灰色。

angcyo commented 4 years ago

根据您的描述, 以下代码能帮助您:

tabLayout.apply {
    configTabLayoutConfig {
        onSelectIndexChange = { fromIndex, selectIndexList, reselect ->
            val toIndex = selectIndexList.first()
            if (toIndex < 3) {
                tabSelectColor = Color.WHITE
                tabDeselectColor = Color.parseColor("#999999")
            } else {
                tabSelectColor = Color.RED
                tabDeselectColor = Color.YELLOW
            }
            tabLayout.dslSelector.updateStyle()
        }
    }
}

注意tabLayout.dslSelector.updateStyle()这个是生效的关键,而非invalidate,之前回复有问题.

Demo代码已更新, 可以fetch查看一下.

YumikoAzu commented 4 years ago

根据您的描述, 以下代码能帮助您:

tabLayout.apply {
    configTabLayoutConfig {
        onSelectIndexChange = { fromIndex, selectIndexList, reselect ->
            val toIndex = selectIndexList.first()
            if (toIndex < 3) {
                tabSelectColor = Color.WHITE
                tabDeselectColor = Color.parseColor("#999999")
            } else {
                tabSelectColor = Color.RED
                tabDeselectColor = Color.YELLOW
            }
            tabLayout.dslSelector.updateStyle()
        }
    }
}

注意tabLayout.dslSelector.updateStyle()这个是生效的关键,而非invalidate,之前回复有问题.

Demo代码已更新, 可以fetch查看一下.

ok~效果实现了,感谢解答~!