简体   繁体   中英

Android activity -> fragment -> viewPager with tabLayout not working

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM