我得到了 3 个标签,但是我得到了一个 fragment 。是否可以为 3 个选项卡使用一个 fragment ?
目前我的 FragmentPagerAdapter 中有这个:
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new FragmentTwo(region,"1");
} else if(position == 1){
return new FragmentTwo(region,"2");
}
return new FragmentTwo(region,"3");
}
但只在一个选项卡中显示所有数据..
我已经编辑了我的代码,但仍然在一个选项卡中获取所有数据。 目前:
@Override
public Fragment getItem(int position) {
return FragmentTwo.newInstance(region, position);
}
&&
static FragmentTwo newInstance(String region,int position) {
FragmentTwo frag=new FragmentTwo();
Bundle args=new Bundle();
args.putInt("KEY_POSITION", position);
args.putString("REGION", region);
if(position == 0){
args.putString("TYPE","A");
}
else if(position == 0){
args.putString("TYPE","B");
}
else{
args.putString("TYPE","C");
}
frag.setArguments(args);
return(frag);
}
为了创建我的 FragmentPagerAdapter,我调用了 getSupportFragmentManager();
最佳答案
是的,您可以创建 FragmentTwo 的实例,将其保存在字段/变量中,并将其用作函数中的返回数据。
需要注意的一件事是,您不应该为 fragment 使用带参数的构造函数。将 Bundle 与参数一起使用。带有参数的构造函数可能会导致 Fragments 出现问题。
看看这个帖子:Best practice for instantiating a new Android Fragment
关于android - 为多个选项卡返回相同的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28055732/