-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBottomNavigationView.kt
39 lines (35 loc) · 1.37 KB
/
BottomNavigationView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.bottomnavigation.extension
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
import android.util.Log
/**
* Created by Yasuhiro Suzuki on 2017/07/15.
*/
/**
* This can be used in Support Library 27 or lower.
* See http://stackoverflow.com/questions/40176244/how-to-disable-bottomnavigationview-shift-mode
*/
fun BottomNavigationView.disableShiftMode() {
val menuView = getChildAt(0) as BottomNavigationMenuView
try {
menuView.javaClass.getDeclaredField("mShiftingMode").also { shiftMode ->
shiftMode.isAccessible = true
shiftMode.setBoolean(menuView, false)
shiftMode.isAccessible = false
}
for (i in 0 until menuView.childCount) {
(menuView.getChildAt(i) as BottomNavigationItemView).also { item ->
item.setShiftingMode(false)
item.setChecked(item.itemData.isChecked)
}
}
} catch (t: Throwable) {
Log.e("BottomNavigationHelper", "Unable to get shift mode field", t)
} catch (e: IllegalAccessException) {
Log.e("BottomNavigationHelper", "Unable to change value of shift mode", e)
}
}
fun BottomNavigationView.active(position: Int) {
menu.getItem(position).isChecked = true
}