Aslami.dev
Aslami.dev

Reputation: 1030

cannot use `BottomNavigationView` and `ViewPager` and `TabLayout` simultaneously

I did so much researches in the internet and the conclusion was that we cannot use BottomNavigationView and ViewPager and TabLayout simultaneously. but there are a lot of app such as Instagram that merged them. my question is that how can they do that? do they use a special trick or not?

Upvotes: 3

Views: 336

Answers (2)

rtour mobil
rtour mobil

Reputation: 36

you can create bottomNavigationView in activity and attach fragment in it with this code when click a on item of it:

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction()
                .replace(frameId, fragment, "fragTransaction");
        transaction.commit();

and then create a fragment wiht a tabLayout and viewPager to control child fragment in it and you most using to control fragment in viewPagerAdapter

    FragmentManager fragmentManager = getChildFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction()
            .replace(frameId, fragment, "fragTransaction");
    transaction.commit();

Upvotes: 1

Maksym V.
Maksym V.

Reputation: 2937

It is not difficult to use bottomNavigationView with ViewPager.

  1. In your layout declare both of these controls.
  2. Setup viewPageAdapter and add fragments to it, set adapter to viewPager.
  3. Setup bottomNavigationView overriding onNavigationItemSelectedListener, which now will be setting current item of viewPagerAdapter.

Do you need more detailed code examples, or you are ready to rock?

Upvotes: 1

Related Questions