显示Viewpager的特定片段页面

时间:2018-04-13 09:35:59

标签: android android-viewpager fragment

我想在onCreateView中打开viewpager的特定片段。我怎么能这样做。我的应用程序总是开始位置0 ..但我必须打开例如3页...当我打开我的应用程序的第三个元素必须显示viewpager ..我怎么做?

private void setupViewpager(ViewPager viewPager) {
    MyAdapter adapter = new MyAdapter(getChildFragmentManager());
    adapter.addFragement(new Fragment_home1());
    adapter.addFragement(new Fragment_home2());
    adapter.addFragement(new Fragment_home3());
    adapter.addFragement(new Fragment_home4());
    adapter.addFragement(new Fragment_home5());
    adapter.addFragement(new Fragment_home6());
    adapter.addFragement(new Fragment_home7());

    viewPager.setAdapter(adapter);

}

private void setupTabName() {
    tabLayout.getTabAt(0).setText(R.string.hammasi);
    tabLayout.getTabAt(1).setText(R.string.amalgaOshirish);
    tabLayout.getTabAt(2).setText(R.string.imzolash);
    tabLayout.getTabAt(3).setText(R.string.tasdiqlash);
    tabLayout.getTabAt(4).setText(R.string.malumot);
    tabLayout.getTabAt(5).setText(R.string.rezolutsiya);
    tabLayout.getTabAt(6).setText(R.string.toldirishga);

}

      private class MyAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();

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

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

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

    public void addFragement(Fragment fragment) {
        mFragmentList.add(fragment);
    }


}

2 个答案:

答案 0 :(得分:0)

你可以这样做:viewPager.setCurrentItem(3);

答案 1 :(得分:0)

这是我的全部答案

viewPager.setAdapter(viewPagerAdapter);

首先使用setAdapter,然后使用

viewPager.setCurrentItem(1);