0

I have a main Activity. In it I use a navigationView. If I change menu items, the fragment contained in the activity will be changed.

Now, I have a menu item called news. I have different kind of news, so I want to use TabLayout with ViewPager in NewsFragment. The tabLayout is not visible tho, and I know the reason, why. It is because in the main Activity I use a navigationBar and AppBarLayout. (I know, it is the reason from the following post: TabLayout not showing tabs after adding navigation bar

Does anyone have a workaround for this problem?

Zsolt Nagy
  • 13
  • 4

1 Answers1

0

I found myself the answer. I just needed to add the TabLayout inside the AppBarLayout, and in the fragment's onCreateView method I could do this:

View layout = inflater.inflate(R.layout.fragment_news_page, container, false);
ViewPager viewPager = layout.findViewById(R.id.news_viewPager);
// initialize adapter
viewPager.setAdapter(adapter);

TabLayout tabLayout = getActivity().findViewById(R.id.tabLayout);
tabLayout.setVisibility(View.VISIBLE);
tabLayout.setupWithViewPager(viewPager);
Zsolt Nagy
  • 13
  • 4