我正在开发Android屏幕,以使用tablayout
和viewpager
显示2个不同的仪表板,但在两个不同的标签(即MyDashboard
和TeamDashboard
上仅显示一个屏幕)请检查我的代码。我是android开发的新手。
我尝试将viewpager
与pageradapter
一起使用以滑动屏幕。
public class ViewPagerAdapter extends FragmentPagerAdapter {
private int position;
private String tabTitles[] = new String[]{"MyDashboard", "Team` `Dashboard"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
// MyDashboard_Fragment dashboard_fragment=new `MyDashboard_Fragment();
// TeamDashboard_Fragment dashboard_fragment1=new `TeamDashboard_Fragment();
// position = position + 1;
// Bundle bundle=new Bundle();
// bundle.putString("message","Fragment :" +position);
// dashboard_fragment.setArguments(bundle);
// return dashboard_fragment;
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MyDashboard_Fragment();
break;
case 1:
fragment = new TeamDashboard_Fragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
答案 0 :(得分:0)
问题是因为您没有在使用新值之前将其分配给位置变量,请像下面这样更新代码
public class ViewPagerAdapter extends FragmentPagerAdapter {
private int position;
private String tabTitles[] = new String[]{"MyDashboard", "Team` `Dashboard"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
// MyDashboard_Fragment dashboard_fragment=new `MyDashboard_Fragment();
// TeamDashboard_Fragment dashboard_fragment1=new `TeamDashboard_Fragment();
// position = position + 1;
// Bundle bundle=new Bundle();
// bundle.putString("message","Fragment :" +position);
// dashboard_fragment.setArguments(bundle);
// return dashboard_fragment;
position=i;
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MyDashboard_Fragment();
break;
case 1:
fragment = new TeamDashboard_Fragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}