如何在片段中的onCreateView上更改我的标签页

时间:2017-02-16 06:10:59

标签: android fragmentpageradapter

如果我有三个标签(A,B,C),那么在onCreateView上如何显示B标签而非A标签?

我尝试在代码中使用viewPager.setCurrentItem(1);,但似乎并不那么容易。

是否有人可以给我任何建议,我们将非常感谢任何帮助。

我的标签ViewPagerAdapter:

public class ViewPagerDataReportAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> stringList = new ArrayList<>();
    private int numOfTabs;

    public ViewPagerDataReportAdapter(FragmentManager manager, int numOfTabs) {
        super(manager);
        this.numOfTabs = numOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0://my tab A
                DataReportForBloodPressure bloodPressure = new DataReportForBloodPressure();
                return bloodPressure;
            case 1://my tab B
                DataReportForBloodSugar bloodSugar = new DataReportForBloodSugar();
                return bloodSugar;
            case 2://my tab C
                DataReportForBodyWeight bodyWeight = new DataReportForBodyWeight();
                return bodyWeight;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return numOfTabs;
        //return fragmentList.size();
    }

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

我的片段:

public class DataReportThreePages extends Fragment {

    private View view;
    private TabLayout tabLayout;

    public DataReportThreePages() {
        // Required empty public constructor
    }

    public static DataReportThreePages newInstance(){return new DataReportThreePages();}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//I want to open B tab not A tab when onCreateView 
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_data_report_three_pages, container, false);
            tabLayout=(TabLayout)view.findViewById(R.id.tabLayoutDataReport);
            tabLayout.addTab(tabLayout.newTab().setText(R.string.bloodPressure));//setText for A
            tabLayout.addTab(tabLayout.newTab().setText(R.string.bloodSugar));//setText for B
            tabLayout.addTab(tabLayout.newTab().setText(R.string.bodyWeight));//setText for C
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);



            final ViewPager viewPager=(ViewPager)view.findViewById(R.id.viewPagerDataReport);

            viewPager.setCurrentItem(1);//it's no working

            ViewPagerDataReportAdapter viewPagerAdapter=new ViewPagerDataReportAdapter(getChildFragmentManager(),tabLayout.getTabCount());
            viewPager.setAdapter(viewPagerAdapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            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) {

                }
            });

        }
        return view;
    }

3 个答案:

答案 0 :(得分:2)

你应该设置

viewPager.setCurrentItem(1);

viewPager.setAdapter(viewPagerAdapter);

答案 1 :(得分:1)

设置:

viewPager.setCurrentItem(1);

答案 2 :(得分:1)

使用此行

viewPager.setCurrentItem(1);

在下面的代码之后,因为它在查看寻呼机的设置适配器之后设置了当前项目。

ViewPagerDataReportAdapter viewPagerAdapter=new ViewPagerDataReportAdapter(getChildFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(viewPagerAdapter);

快乐编码