Tablayout+view pager is not displaying the fragment in position 0

半腔热情 提交于 2019-12-03 21:19:16

You should call:

viewPagerVP.setCurrentItem(0);

on onCreate function of MainActivity.

I know the thread is not live but this might help someone and save a day which I had to spend. I ran into the same problem. The problem was same viewIDs on both fragments. Definitely, viewpager and fragment should have handled this but as somehow they didn't.

For me this was happening for two different fragments namely FragmentHome and FragmentNotification. Both had to show data using recyclerView. Home was first fragment in left and next was notification. But first was shown blank and home content was shown on next tab where notification content was expected. As both had same recyclerViews, I wrote a layout_recycler_wrapper.xml to reuse the code. In which a RelativeLayout (R.id.rl_wrapper) was there with a recyclerView. Root cause was I was including a this layout_recycler_wrapper in both fragments layout files using <include>. So same ID (R.id.rl_wrapper) of that wrapper viewGroup (RelativeLayout) were used more than once.

Then I wrote wrapper code directly in fragment's xml files instead of using <include> tag. Then changed id of RelativeLayout wrappers as R.id.rl_wrapper_home and R.id.rl_wrapper_notification in respective files.

On the next run, first fragment was magically FragmentHome. Yahooo.....:)

So ultimately, use of views with same ID (even if in different fragments) was creating problem. This is one possible reason. Check your XML file and remove possible reuses of IDs, that might solve problem for you too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!