Closed Lemou-Memo closed 1 month ago
Hello @Lemou-Memo 👋
Thank you for using the library.
You can try to use selectItem from Menu object.
So basically, the code should be similar to the snippet below:
bottomBar.menu.selectItem(itemId)
Let me know whether you have any further questions.
Best, Alex
@Lemou-Memo
Did it help?
ids = new int[]{R.id.home, R.id.likes, R.id.bookmarks, R.id.settings};
// Set the listener for the bottom navigation bar
expandableBottomBar.setOnItemSelectedListener((view, item, byUser) -> {
if (!isBottomBarItemSelected) {
int index = item.getId();
if (R.id.home == index) {
viewPager2.setCurrentItem(0, true);
} else if (R.id.likes == index) {
viewPager2.setCurrentItem(1, true);
} else if (R.id.bookmarks == index) {
viewPager2.setCurrentItem(2, true);
} else if (R.id.settings == index) {
viewPager2.setCurrentItem(3, true);
}
Log.d(TAG, "selected: " + item.toString());
}
return null;
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
isBottomBarItemSelected = true; // Set flag to avoid duplicate selection
expandableBottomBar.getMenu().select(ids[position]);
isBottomBarItemSelected = false; // Reset flag after selection
}
});
Your method helped me a lot, and I perfectly implemented this feature this way.
Hello,
I'm currently using ExpandableBottomBar together with ViewPager2 in my project, and I've encountered a problem. It seems that ExpandableBottomBar does not provide a method to programmatically select a specific item. I am wondering if this is an issue with my usage or if this functionality is not supported by the component itself.
I need to dynamically switch to a specific item in the bottom bar, for example, when the ViewPager2 page changes, I want to update the selected item in the bottom navigation bar accordingly. I have not found an API that allows me to do this.
Code Example Here is the code I am currently using:
Questions:
Is there a method in ExpandableBottomBar to programmatically select a specific item? If not, are there alternative ways to achieve this functionality? Is this a limitation of ExpandableBottomBar, or is there another recommended approach? Thank you for your help!
Environment:
ExpandableBottomBar version: expandablebottombar:1.5.3 Thanks!