-
Notifications
You must be signed in to change notification settings - Fork 1
/
RootFragment.kt
165 lines (137 loc) · 6.08 KB
/
RootFragment.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.zhuinden.bottomnavfragmentexample
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.AccessibilityDelegateCompat
import androidx.core.view.ViewCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionItemInfoCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.zhuinden.bottomnavfragmentexample.databinding.RootFragmentBinding
import com.zhuinden.bottomnavfragmentexample.utils.viewBinding
import com.zhuinden.simplestackextensions.fragments.KeyedFragment
class RootFragment : KeyedFragment(R.layout.root_fragment) {
private val binding by viewBinding(RootFragmentBinding::bind)
private lateinit var firstFragment: FirstFragment
private lateinit var secondFragment: SecondFragment
private lateinit var thirdFragment: ThirdFragment
private val fragments: Array<Fragment>
get() = arrayOf(
firstFragment,
secondFragment,
thirdFragment
)
private var selectedIndex = 0
private val tabs: Array<TextView>
get() = binding.run {
arrayOf(buttonFirstTab, buttonSecondTab, buttonThirdTab)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
val firstFragment = FirstFragment().also { this.firstFragment = it }
val secondFragment = SecondFragment().also { this.secondFragment = it }
val thirdFragment = ThirdFragment().also { this.thirdFragment = it }
childFragmentManager.beginTransaction()
.add(R.id.containerBottomNavContent, firstFragment, "firstFragment")
.add(R.id.containerBottomNavContent, secondFragment, "secondFragment")
.add(R.id.containerBottomNavContent, thirdFragment, "thirdFragment")
.selectFragment(selectedIndex)
.commit()
} else {
selectedIndex = savedInstanceState.getInt("selectedIndex", 0)
firstFragment = childFragmentManager.findFragmentByTag("firstFragment") as FirstFragment
secondFragment =
childFragmentManager.findFragmentByTag("secondFragment") as SecondFragment
thirdFragment = childFragmentManager.findFragmentByTag("thirdFragment") as ThirdFragment
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = binding
ViewCompat.setAccessibilityDelegate(
binding.containerBottomNavItems,
object : AccessibilityDelegateCompat() {
override fun onInitializeAccessibilityNodeInfo(
host: View,
info: AccessibilityNodeInfoCompat
) {
super.onInitializeAccessibilityNodeInfo(host, info)
info.setCollectionInfo(
CollectionInfoCompat.obtain(
1, tabs.size, false, CollectionInfoCompat.SELECTION_MODE_SINGLE
)
)
}
}
)
binding.containerBottomNavItems.importantForAccessibility =
View.IMPORTANT_FOR_ACCESSIBILITY_YES
tabs.forEachIndexed { index, textView ->
textView.setOnClickListener {
selectFragment(index)
}
ViewCompat.setAccessibilityDelegate(textView, object : AccessibilityDelegateCompat() {
override fun onInitializeAccessibilityNodeInfo(
host: View,
info: AccessibilityNodeInfoCompat,
) {
super.onInitializeAccessibilityNodeInfo(host, info)
//info.className = Button::class.java.name // use this if you want to make it be called "Button"
@SuppressLint("PrivateResource")
info.roleDescription = getString(com.google.android.material.R.string.item_view_role_description) // use this if you want to make it be called "Tab"
info.setCollectionItemInfo(
CollectionItemInfoCompat.obtain(
1,
1,
index,
1,
false,
index == selectedIndex
)
)
info.isSelected = index == selectedIndex
}
})
}
setupTabSelectedState(selectedIndex)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("selectedIndex", selectedIndex)
}
private fun FragmentTransaction.selectFragment(selectedIndex: Int): FragmentTransaction {
fragments.forEachIndexed { index, fragment ->
if (index == selectedIndex) {
attach(fragment)
} else {
detach(fragment)
}
}
return this
}
private fun setupTabSelectedState(selectedIndex: Int) {
tabs.forEachIndexed { index, textView ->
textView.setTextColor(
when {
index == selectedIndex -> ContextCompat.getColor(
requireContext(),
R.color.tab_selected
)
else -> ContextCompat.getColor(requireContext(), R.color.tab_unselected)
}
)
}
}
private fun selectFragment(indexToSelect: Int) {
this.selectedIndex = indexToSelect
setupTabSelectedState(indexToSelect)
childFragmentManager.beginTransaction()
.selectFragment(indexToSelect)
.commit()
}
}