st235 / ExpandableBottomBar

A new way to implement navigation in your app 🏎
MIT License
783 stars 58 forks source link

No method to programmatically select a specific item in ExpandableBottomBar #103

Closed Lemou-Memo closed 1 month ago

Lemou-Memo commented 1 month ago

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:

expandableBottomBar.setOnItemSelectedListener((view, item, byUser) -> {
    int index = item.getId();
    switch (index) {
        case R.id.home:
            viewPager2.setCurrentItem(0, true);
            break;
        case R.id.likes:
            viewPager2.setCurrentItem(1, true);
            break;
        case R.id.bookmarks:
            viewPager2.setCurrentItem(2, true);
            break;
        case R.id.settings:
            viewPager2.setCurrentItem(3, true);
            break;
        default:
            break;
    }
    return null;
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
    @Override
    public void onPageSelected(int position) {
        super.onPageSelected(position);
        // Trying to set the selected item, but no method found
        // expandableBottomBar.setSelectedItemId(R.id.some_id); // Example code
    }
});

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!

st235 commented 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

st235 commented 1 month ago

@Lemou-Memo

Did it help?

Lemou-Memo commented 1 month ago
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.