0

I have 4 fragments in a ViewPager. 3 of the have static data, the other one is loading data from a remote server. When I run the program, all the fragments with static RecyclerView data load perfectly fine. But the one with dynamic data won't show until I navigate away from the page and then reselect the tab (or navigate back).

I have tried solutions explained here Update ViewPager dynamically? but I can't get it to work

private void initialize() {

    //Create fragments and setup viewpager
    frag_categories = new Fragment_Categories();
    frag_deals =  new Fragment_Deals();
    frag_favourites = new Fragment_Favorites();
    frag_sangwapo = new Fragment_Sangwapo();

    setupViewPager(viewPager);

    //prepare tabLayout
    tabLayout =  findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
    tabLayout.getTabAt(1).select();
    tabLayout.addOnTabSelectedListener(this);
}


private void setupViewPager(ViewPager viewPager) {
    adapter = new AdapterViewPager(getSupportFragmentManager());
    adapter.addFragment(frag_favourites, "Stores");
    adapter.addFragment(frag_categories, "Categories");
    adapter.addFragment(frag_deals, "Deals");
    adapter.addFragment(frag_sangwapo, "Sangwapo");
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(0);
}

//Inside the fragment not refreshing

adapterSangwapo = new AdapterSangwapo(getActivity(),list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapterSangwapo);
        updateListItems();

where updateListItems is populating an ArrayList (list) and calling notifyDataSetChanged on adaptersangwapo

Even when i try viewPager.setOffscreenPageLimit(3);, it still wont refresh until i navigate away. I have also tried both FragmentPagerAdapter and FragmentStatePagerAdapte

There are no errors being displayed.

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
Kasalwe
  • 358
  • 2
  • 6
  • Try to call `notifyDataSetChanged` when your fragment becomes visible again thanks to ViewPager listener. – cesarmarch Jun 18 '19 at 16:54
  • I have tried calling notifyDataSetChanged (on adapter for ViewPager) inside onPageSelected but still nothing. Its like its just maintaining the blank fragment. Funny enough, when i put static data, its working. I have even tried notifyDataSetChanged on recyclerView adapter inside setUserVisibleHint but nothing (just crashing views) – Kasalwe Jun 18 '19 at 17:39
  • Is the data well updated ? Like the loading from the server is done at the right time ? Did you also try to use `POSITION_NONE` like specified in your link ? – cesarmarch Jun 18 '19 at 18:29
  • Yes i tried POSITION_NONE. I tried loading the data both in onCreateView and onResume. In all cases, the data would only show if i navigate away from the fragment and back. – Kasalwe Jun 18 '19 at 18:40

0 Answers0