aurelhubert / ahbottomnavigation

A library to reproduce the behavior of the Bottom Navigation guidelines from Material Design.
3.83k stars 682 forks source link

First fragment only shown when its tab is reselected #372

Closed Deandrew007 closed 6 years ago

Deandrew007 commented 6 years ago

Hey, lovely lib. My first fragment isn't visible unless another tab is clicked, then reselect the first tab or the first tab is clicked twice. Which is when the fragment is shown. Any possible suggestions?

jahirfiquitiva commented 6 years ago

You probably need to call the bottom navigation item selection method, and also fire the onClickListener so it shows the Fragment

Deandrew007 commented 6 years ago

Here's a snippet of my code.

` bottomNavigationBar.setCurrentItem(0); bottomNavigationBar.setOnTabSelectedListener((position, wasSelected) -> {

        if (position == SHOP_FRAGMENT)
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new ShopFragment()).commit();
        }
        else if (position == MESSAGES_FRAGMENT)
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MessagesFragment()).commit();
        }
        else if (position == PROFILE_FRAGMENT)
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new ProfileFragment()).commit();
        }
        return true;
    });` 
jahirfiquitiva commented 6 years ago

I haven't tested but I think putting bottomNavigationBar.setCurrentItem(0); after setting the listener, should work.

Like so:

bottomNavigationBar.setOnTabSelectedListener((position, wasSelected) -> {
    ...
});
bottomNavigationBar.setCurrentItem(0);

As a suggestion, when posting multiple lines of code wrap it like this:


```java
System.out.println("Hello, World!");
```

Deandrew007 commented 6 years ago

Your solution was a fix. Thanks a lot and also for the suggestion.