如何在Android中使用viewpager创建动态选项卡?

2022-09-03 06:37:23

解释:

假设,我有多个类别来自我的REST。我不知道它的垫子有多少个类别是5,7,有时是2等。我想用视图页服务器将所有类别放在我的选项卡上。我想根据类别大小创建一个选项卡。让我们看看例如,假设在我的REST响应中只有2个类别,它只创建两个选项卡,如果它有5个类别,则5个选项卡,依此类推。

下面是创建选项卡的示例测试示例。

主要活动.java

public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tabLayout=(TabLayout)findViewById(R.id.tabs);
        viewPager=(ViewPager)findViewById(R.id.viewpager);

        setupViewPager(viewPager);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);
    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        for(int i=0;i<3;i++){
            adapter.addFragment(new OneFragment(), "ONE");
//            adapter.addFragment(new TwoFragment(), "TWO");
            viewPager.setAdapter(adapter);
        }
    }
}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

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

    public ViewPagerAdapter(FragmentManager manager){
        super(manager);
    }

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

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment,String title){
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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

拜托,我该如何解决???


答案 1

获得类别大小后,您可以根据类别大小添加动态选项卡,如下所示:

public class MainActivity extends AppCompatActivity {

TabLayout tabLayout;
ViewPager viewPager;
Toolbar toolbar;
int no_of_categories=-1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar=(Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    tabLayout=(TabLayout)findViewById(R.id.tabs);
    viewPager=(ViewPager)findViewById(R.id.viewpager);

    no_of_categories=YOUR_NO_CATEGORIES;

    for (int i = 0; i < no_of_weeks; i++) {
        tabLayout.addTab(tabLayout.newTab().setText("TAB " + String.valueOf(i + 1)));
    }

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });


   }
}

您的适配器类应如下所示:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    int mNumOfTabs;
    Fragment fragment = null;

    public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        for (int i = 0; i < mNumOfTabs ; i++) {
            if (i == position) {
                fragment = YourFragment.newInstance();
                break;
            }
        }
        return fragment;

    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
}

答案 2

如果我是你,我会尝试这样的事情:

它不是一个完美的解决方案,只是一个你如何做到这一点的想法,你需要进一步修改它。

创建并命名它们,如 ,依此类推,以占类的总数。number of fagments = total number of categeriesfragment0fragment1

然后先得到分类的数量

int count = number of categeries;

然后添加片段来像这样ViewPager

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    for(int i=0; i <= count; i++){
        adapter.addFragment(new Fragment(i)(), "ONE");
        viewPager.setAdapter(adapter);
    }
}

并命名片段,即选项卡标题,您可以制作片段标题列表,如下所示

private final String[] title = {"One", "Two","Three"};

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        for(int i=0; i < count; i++){
            adapter.addFragment(new Fragment(i)(), title[i]);
            viewPager.setAdapter(adapter);
        }
    }

我希望它有帮助


推荐