Search code examples
androidandroid-viewpagerandroid-navigationview

ViewPager controlled by NavigationView


I have a navigation drawer in my app, and I want choosing one of it's options will show the wanted fragment in the ViewPager. It can't work, though, without an OnPageChangeListener, that I know how to set up with a TabLayout, but couldn't find how to do it with the navigation drawer itself. Right now, choosing an option from the navigation drawer doesn't change the preview of the ViewPager.

I've searched for it on the web, but no solution was found, only issues and tutorials about using ViewPager with TabLayout and/or with the navigation drawer' but in these cases the OnPageChangeListener gets "new TabLayout.TabLayoutOnPageChangeListener(tabLayout)" as an OnPageChangeListener

public void setUpPageAdapter(){
pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); --> what should replace the TabLayoutOnPageChangeListener?
}

Solution

  • Eventually I've solved my problem - I couldn't find a straightforward solution, so just used a TabLayout with height of 0dp, so it will exist but won't be seen to the user, a tutorial to make a ViewPager controlled by tablayout and connecting it to a navigation drawer can be found all over the internet, so just hiding the TabLayout works just fine (: