简体   繁体   中英

TabLayout Fragment Issue

Current layout

The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:

this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();

Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..

Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:

CompletableFuture.runAsync(this::prepare);

recyclerView.post(() ->
{
   adapter.list = newList;
   adapter.notifyDataSetChanged();
   progress.setVisibility(View.INVISIBLE);
});

Now the issue that I am facing:

Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.

TabViewerAdapter extends FragmentStatePagerAdapter

The adapter is initialised using ChildFragmentAdapter as below:

TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());

I know behaviour "0" is deprecated so tried with "1" as well as below:

TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());

Tried invalidating views, notifying adapter and many other.. No use.

I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.

This means, I am missing something and tried the onResume method. But didn't work.

Am I doing something stupid or against design patterns or conventions? No idea what to do..

Please help, thank you.

I resolved this issue by doing the things below:

  1. Used a SharedViewModel to handle the list
  2. Used ListAdapter for RecyclerView

All good now.

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