Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to add/remove tabs in ViewPager at run time?

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.v(TAG, "onCreateView");

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    // setting up the view pager and tab layout
    mViewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
    mTabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout);
    PageAdapter adapter = new PageAdapter(getFragmentManager());
    mViewPager.setAdapter(adapter);

    mTabLayout.setupWithViewPager(mViewPager);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

    return rootView;
}

My custom pager adapter looks like this:

public class PageAdapter extends FragmentStatePagerAdapter {

    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new GroupTabFragment(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String title = "";
        switch (position) {
            case 0:
                title = "Game";    // this is from a template, i have to
                break;             // return title dynamically
            case 1:
                title = "Movie";
                break;
        }
        return title;
    }

    @Override
    public int getCount() {
        return 2;                  // the tabs number is a subject of change
    }                              // i don't know it from the begining
}

The problem i have is that i have to add or remove a tab when a button is clicked, or something like this.

Each tab will display a classic list of objects. Thank you.

like image 796
Lucian Iacob Avatar asked Mar 12 '23 19:03

Lucian Iacob


2 Answers

There are 2 ways doing this although ViewPager is not suitable for dynamic removing:

1) Pass ArrayList<Fragment> data as parameter to PageAdapter

Then Adapter will look like this:

public class PageAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> data;

public PageAdapter(FragmentManager fm, ArrayList<Fragment> data) {
    super(fm);
    this.data = data;
}

@Override
public Fragment getItem(int position) {
    return data.get(position);
}

@Override
public CharSequence getPageTitle(int position) {
    String title = "";
    switch (position) {
        case 0:
            title = "Game";    // this is from a template, i have to
            break;             // return title dynamically
        case 1:
            title = "Movie";
            break;
    }
    return title;
}

@Override
public int getCount() {
    return data.size();                  // the tabs number is a subject of change
}                              // i don't know it from the begining

}

to remove item you do in Activity\Fragment:

data.remove(fragmentId);
adapter.notifyDataSetChanged();

2) Recreate adapter every time you want to delete item. So you basically create new adapter with new data.

data.remove(fragmentId);
PageAdapter adapter = new PageAdapter(getFragmentManager(), data);
mViewPager.setAdapter(adapter);
like image 162
AnZ Avatar answered Apr 06 '23 06:04

AnZ


Instead of using mTabLayout.setupWithViewPager(mViewPager); you can setup your tabs manually:

mTabLayout.addTab(mTabLayout.newTab().setText("Movie"));
mTabLayout.addTab(mTabLayout.newTab().setText("Game"));
mTabLayout.addTab(mTabLayout.newTab().setText("Audio"));
mTabLayout.setOnTabSelectedListener(this);

Removing is also easy: removeTabAt(int position) or removeTab(Tab tab)

like image 45
rafakob Avatar answered Apr 06 '23 08:04

rafakob