Search code examples
androidandroid-studioandroid-viewpagerfragment

How to handle dynamic multiple nested tab layout fragments dynamically?


I have an array of items from an Api, I have to distribute them into multiple tabs group by a sub-item hierarchically.

The number of levels and items are dynamic.

The problem is why tabs load again and again and it takes too long.


Solution

  • Anyway, this may help you

    ViewPager pager;
    ViewPagerAdapter adapter;
    private List<Fragment>  fragments;
    ArrayList<String> Titles;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            fragments = new ArrayList<Fragment>();
    
        Titles= new ArrayList<String>();
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, fragments);
        pager.setAdapter(adapter);
        for (int i=0;i<numoffragments;i++){
                fragments.add(new DemoFragment());
    
            }
        adapter.notifyDataSetChanged();
    
         }
    

    You may need to do some more adjustments to get the numoffragments you will be using and also you have to make a fragment that can be used for all the childs

    If your scrolling is too slow you have to set:

    yourViewPager.setOffscreenPageLimit(your_tabs_count);
    

    hope to be useful.