如何在 Fragment 中的 onCreateView 时更改我的标签页
How to change my tab page when onCreateView in Fragment
如果我有三个选项卡(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;
}
你应该设置
viewPager.setCurrentItem(1);
之后
viewPager.setAdapter(viewPagerAdapter);
设置:
viewPager.setCurrentItem(1);
使用这条线
viewPager.setCurrentItem(1);
在下面的代码之后,因为它在设置视图分页器的适配器之后设置当前项。
ViewPagerDataReportAdapter viewPagerAdapter=new ViewPagerDataReportAdapter(getChildFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(viewPagerAdapter);
快乐编码
如果我有三个选项卡(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;
}
你应该设置
viewPager.setCurrentItem(1);
之后
viewPager.setAdapter(viewPagerAdapter);
设置:
viewPager.setCurrentItem(1);
使用这条线
viewPager.setCurrentItem(1);
在下面的代码之后,因为它在设置视图分页器的适配器之后设置当前项。
ViewPagerDataReportAdapter viewPagerAdapter=new ViewPagerDataReportAdapter(getChildFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(viewPagerAdapter);
快乐编码