N
N
niorx2016-08-21 17:00:52
Android
niorx, 2016-08-21 17:00:52

How to generate scrollable tabs?

The idea and the main difficulty is that you need to generate scrollable tabs when you open an activity by reading their names from the database. The number of these tabs and the names can be different. The elements inside the tabs are all the same by type, they just contain different information. All the guides on the Internet about these very tabs describe working only with a predetermined number of them, when I don’t know how many of them I will have. Any help would be helpful. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2016-08-22
@davidnum95

The point is that while information is being loaded from the database, the PagerAdapter should return one tab with the load. Then, when the infa has loaded, you need to tell the adapter that you need to change the number of tabs.
Something like:
Adapter

public class SomePagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<Tab> tabs;

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

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

    @Override
    public Fragment getItem(int position) {
        if (tabs == null) return new LoadingFragment();
        return tabs.get(position);
    }

    public void setTabs(ArrayList<Tab> tabs) {
        this.tabs= tabs;
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        if (tabs == null) return 1;
        return tabs.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs.get(position).title;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question