I have an app with BottomNavigationView, which has 2 menu items. Both have their Fragments. On the first item in the Fragment I have a RecyclerView. I want to open the second menu item on click on RecyclerView row and also send some parameters. How and where should I do it?
What am I doing now:
In the Fragment, which I show on the first menu item, I have a method, that I pass to RecyclerViewAdapter, so that it can be triggered each time a row is tapped. But I cannot find any method in my listener as BottomNavActivity to open it's second menu item
You need a reference to your activity within the adapter. You can pass this in during initialisation using
SomeAdapter(var activity: Activity)
Then inside your adapter you set the onClickListener with the following action
activity?.navigation?.selectedItemId = R.id.navigation_id_here
From API 25.3.0 it was introduced the method setSelectedItemId(int id) which let's you mark an item as selected as if it was tapped.
From docs:
Set the selected menu item ID. This behaves the same as tapping on an
item.
Code example:
BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setSelectedItemId(R.id.my_menu_item_id);
For those, who still use SupportLibrary < 25.3.0
View view = bottomNavigationView.findViewById(R.id.menu_action_item);
view.performClick();
Related
I have bottomNavigation in my android app as shown below in picture. It works fine if I just click on home, cart and other bottomNavigation Items.
As you can see in picture that user can navigate to all categories in home. If I navigate to AllCategoriesFragment then in home item will be selected in bottomNavigation which is correct behavior. But if I go to cart and then click again on home then it shows AllCategoriesFragment which is also correct behavior but in BottomNavigation it doesnt highlight the home items, instead it keep cart item highlighted.
How I can get rid of this issue? Or how I can highlight the bottomNavigation selected item programmatically?
I did it programmatically like by getting activity reference inside fragment andd then getting navView from it and afterwards set the check state of the item to true
activity?.let {
val navView = it.findViewById<BottomNavigationView>(R.id.nav_view)
navView.menu.getItem(0).setChecked(true)
}
I would like to click on an item of a listview to show another listview of other items, what can I do?
in my application I have a main Activity, where I have inserted a bottom view navigation, when you click on an item in the bottom view menu, the corresponding fragment appears in the main activity.
my listview is in the first fragment, and in the same fragment the next screen must appear, after the click in the listview.
do I have to replace the fragment of the listview with another one or can I replace only the listview? how?
Thanks for the attention
Instead of those options (replacing the fragment / list view), you could use the onItemSelected method to repopulate the list view object with the new list. Since you already know how to do this, it would seem the simplest option. A simple switch statement would suffice.
Navigation view example
In reference to the above example of NavigationView, say I have "Go to Second item" button in First item layout. If I click on "Go to Second item" button, I am successfully able to go to the Second item layout, but the checked item in NavigationView remains in First item. How do I change the checked state of a MenuItem from another class which extends Fragment. If I directly click on Second item from the Menu, then the checked state changes accordingly, but how to change checked state from another class which extends Fragment.
NavigationView tutorial: Tutorial and Android documentation.
Wrap your menu items with the tag group with attribute android:checkableBehavior="single"
Call NavigationView.setCheckedItem() when you click the button.
I have a listview, clicking on one of the item in listview takes me to some fragment (different item may take to different fragment). Once a new fragment is opened I want to swipe to open the the next item in list instead of going back and clicking on the next item. I am thinking of using ViewPagers but not sure how to open the next item in the list.
You can create an Activity which has a ViewPager and Fragments with desired info as its children. On a list item click, take the position info and pass it to the Activity via Bundle. And inside the Activity after setting the ViewPager, you can show the desired Fragment with ViewPager's setCurrentItem(int item) function.
I created a navigation drawer project in android studio and created the separate fragments for my each item in the list view in drawer. However, now the items in the list are not static ones. I have a map in which i have some values based on which i have to choose whether to show a particular item or not.
I tried to change the call to setAdapter method based on values in map. However, it breaks the code when clicked on a particular item because in my fragments i do setOnAttached with a certain position which is hard coded such as 0,1,2 .. My question is how can I change all of this to show the corresponding fragment when any item in drawer is clicked.
Is it possible to know that this fragment should be attached to the dynamically calculated position based on the list view that I get at run time ?
public void onAttach(Activity activity) {
((MyActivity)activity).onSectionAttached(4);
}
Thanks