Issue
After spending a lot of time and not finding a suitable solution, I decided to turn to this service for help.
I have an example of the simplest application that implements Drawer. This example contains only menu items without any implementations. In the menu item Share, I added a Switch and a listener to it:
main_nav.xml
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
android:title="@string/menu_share" />
MainActivity.kt
val menuItem = navigation_view.menu.findItem(R.id.nav_share)
val switch_id = menuItem.actionView as SwitchCompat
switch_id.isChecked = true
switch_id.setOnClickListener {
// TODO: empty scope
}
The switch works smoothly and with animation as shown below:
But now I need to turn off the Home menu item if the Switch is off:
switch_id.setOnClickListener {
navigation_view.menu.findItem(R.id.nav_home).isEnabled = switch_id.isChecked
}
After adding this line, my Switch animation breaks!
I spent two days solving this issue and in the end to no avail.
My question: WTF with android menu in 2021? Why does accessing a menu item break the animation and how can I fix it? I would be grateful for any help !!
Solution
I created an issue with this question in Android Tracker and got an answer:
Either way, this is likely to be a non-trivial fix for an issue that is not a regression and not impacting correctness of the UI. At this time, we have no plans to address the issue.
And then a workaround was suggested:
Something along the lines of adding a setOnCheckedChangeListener that posts a delayed Runnable to effect the change. I'd recommend asking on StackOverflow if you need a detailed example.
I added a postDelay(200)
and the Switch animation works fine:
switch.setOnCheckedChangeListener { _, isChecked ->
Handler(Looper.getMainLooper()).postDelayed({
navigationView.menu.findItem(R.id.nav_home).isEnabled = isChecked
}, 200)
}
Answered By - Tomas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.