可以在运行时在ViewPager中添加/删除选项卡吗?

时间:2016-03-25 14:04:53

标签: java android android-fragments android-viewpager android-tablayout

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;
}

我的自定义寻呼机适配器如下所示:

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
}

我遇到的问题是,我必须在点击按钮时添加或删除标签,或类似的东西。

每个选项卡都会显示一个经典的对象列表。 谢谢。

3 个答案:

答案 0 :(得分:4)

虽然ViewPager不适合动态删除,但有两种方法可以做到这一点:

1)将ArrayList<Fragment> data作为参数传递给PageAdapter

然后适配器将如下所示:

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

}

删除您在Activity \ Fragment中执行的项目:

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

2)每次要删除项目时重新创建适配器。所以你基本上用新数据创建新的适配器。

data.remove(fragmentId);
PageAdapter adapter = new PageAdapter(getFragmentManager(), data);
mViewPager.setAdapter(adapter);

答案 1 :(得分:0)

您可以手动设置标签,而不是使用mTabLayout.setupWithViewPager(mViewPager);

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

删除也很简单:removeTabAt(int position)removeTab(Tab tab)

答案 2 :(得分:0)

在这里,我找到了一些更好的解决方案,可以从当前上下文中删除所有视图/片段。

这里我在Fragment中使用了TabLayout和ViewPage。 因此,我们需要清除TabLayout中的选项卡,ViewPage中的视图和FragmentManager中的片段。

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    FragmentManager fragmentManager = null;
    public TabLayout tabLayout;
    public ViewPager viewPager;

    public ViewPagerAdapter(FragmentManager manager, TabLayout tabLayout, ViewPager pager) {
        super(manager);
        this.fragmentManager= manager;
        this.tabLayout = tabLayout;
        this.viewPager= pager;
    }

    public void removeAll() {
        tabLayout.removeAllTabs();
        viewPager.removeAllViewsInLayout();
        viewPager.removeAllViews();
        viewPager.setAdapter(null);

        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        if (manager.getFragments() != null) {
            manager.getFragments().clear();
        }
        viewPager.setAdapter(adapterPage);
        viewPager.invalidate();
        viewPager.requestLayout();

        mFragmentList.clear();
        mFragmentTitleList.clear();
    }