菜鸡记录帖:
和ViewPager2联动
fun BottomNavigationView.setUpWithViewPager2(viewPager2: ViewPager2) {
val map = mutableMapOf<MenuItem, Int>()
if (this.menu.size != viewPager2.adapter?.itemCount) error("viewpager2数量需要和BottomNavigationView数量相同")
this.menu.forEachIndexed { index, item ->
map[item] = index
}
viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
this@setUpWithViewPager2.selectedItemId =
this@setUpWithViewPager2.menu.getItem(position).itemId
}
})
this.setOnNavigationItemSelectedListener {
val index = map[it] ?: error("asdasd")
Log.d("TAG", "setUpWithViewPager2: index = $index")
viewPager2.setCurrentItem(index, true)
return@setOnNavigationItemSelectedListener true
}
}
BottomNavigationView选中效果修改和水波纹修改
1.新建color文件夹
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/design_default_color_secondary" android:state_checked="true" />
<item android:color="#999" android:state_checked="false" />
</selector>
1)选中icon:itemIconTint
2)选中字体颜色:itemTextColor
3)水波纹颜色:itemRippleColor
?
|