I have a navigation drawer whose list can change based on an action in the current activity. After making a change in the current activity I update the list immediately in the code. I opened the navigation drawer, but no change has been shown. However if I proceed to change activities and open the drawer again, the change in the list is now updated. This leads me to believe that after the first time the Navigation Drawer is created or called, the drawer is reusing the same view instead of being destroyed and recreated.
I am looking for a method with Android that would be equivalent to onOpenDrawer, however I do not see one in the docs. In the docs there is onDrawerOpened(View view) which is called after the drawer has already been opened. I also have the option to call openDrawer(View view) which then annimates the drawer open.
I am wondering if there is a method that I am missing and that I can override which will allow me to update the listview being displayed in the navigation drawer.
Thank you in advance.
Calling of notifyDatasetChanged() still applies to ListView adapter, regardless of ListView parent.
Related
I have a navigation drawer in my app. Every Entry in the list of the NavigationDrawer represents one navigation graph. I want to mark the element in which the user currently is.
I managed to get the item to be marked after it is selected and until the visible fragment changes (even if the new fragment is still in the same navGraph). I did that by setting a NavigationItemSelectedListener using NavigationView.setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener). Inside this listeners onNavigationItemSelected(item: MenuItem): Boolean function, I do: item.isChecked = true.
The problem with this is that the item is unchecked after the app starts and the item looses it's checked state as soon as the fragment changes (even if the navGraph doesn't change). How do I do this correctly?
I have also tried to set android:checked="true" in the xml declaration of the menu item I want to be checked when the app starts. It doesn't seem to do anything.
I have also tried to set navView.setCheckedItem(R.id.main_nav_option_one) in the Activity's onCreate function. Again, it doesn't seem to do anything.
Can you try
navView.getMenu().getItem(index).setChecked(true)
in the fragment's onResume?
I managed to solve my problem.
The most important change I had to make is mentioned here. I had to give my menu items the same ID as the ID of the navGraphs. When I did this, I removed my NavigationItemSelectedListener because it was interfering with the automatically generated navigation. Without a NavigationItemSelectedListener I could no longer handle a navigation item being clicked, that doesn't have a corresponding element in the navGraph. However I was able to fix it, by adding the NavigationItemSelectedListener back in, and making the correct calls to NavController.navigate() for every element in the list.
TL;DR:
I had to set the menu item IDs to be the same as my navGraph IDs and make the navigation calls myself anyway in the NavigationItemSelectedListener.
Started new project from navigation drawer activity template. By default, all drawer menu items are at the same navigation level. All fragments display hamburger menu button, that shows drawer.
I need to keep all items in drawer, but place Home as top level item and others as it's children like this:
Tried to overwrite toolbar?.setNavigationOnClickListener { } in Gallery fragment, but it also affects Home fragment and I didn't find how to restore default behavior.
How can I set Home as navigation parent for others or how to set navigation click listener to only one fragment?
For example you have set tag for every fragment to trace when you start new fragment and now check with Home and set click event..
Fragment home = getSupportFragmentManager().findFragmentByTag("home");
So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.
i'm quite new to drawer material and i have trouble understanding some things:
I need to create an Activity with a Fragment on it. Different selections on the Drawer must replace the current Fragment with another one, but is the Drawer something in the fragment or it is one for the activity itself. More specifically is the Drawer living in the Fragment and if not is it possible to create it in the Fragment. I am asking this cause i see that when initiating the Drawer you need to fill parent activity. Also when i tried to use the Navigation Drawer template in Android Studio i didn't had the Use a Fragment checkbox.
There are many ways to achieve what you want. But I think the simplest way is:
1) DrawerLayout view should reside in the activity (probably as the base layout).
2) When you click on an item in the draw 2 things happen:
The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it).
The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged().
Don't forget to save your state so it can recover in case the Activity is recreated.
I need to create two MaterialDrawer for the same activity and switch between them. But I need that items be updated when the Drawer is being displayed on the screen.
With the properties we have for MaterialDrawer, the changes between one drawer layout to other only can be visible after the drawer is closed and you touch the drawer toggle again.
Is it possible to do a refresh in the layout, do a invalidate or do a notify in the changes during the display of the drawer, when switching between drawers, for the changes to be displayed on the fly?
Thank you,
Alexandre Bianchi
The MaterialDrawer comes with a easy to use API which will allow you to modify the items, shown in the Drawer at runtime, without the requirement of creating a new Drawer object.
You can remove the current items in the list at any time via the removeAllItems method. https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L747
And you can easily add new items in the same style via the addItems method: https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L756
These changes will directly reflect in the UI.
For some more complex usecases you can also look into the source code used for the AccountSwitcher which will switch 2 lists in the Drawer and animate them: https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/AccountHeaderBuilder.java#L1348