在其中一个活动中我已经将其分段,因为我有一个nil
,我想在其中使用一些片段,但是将片段附加到我的适配器不起作用。
ViewPager
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_food_list, container, false);
...
foods_slider.setAdapter(buildAdapter());
return view;
}
private PagerAdapter buildAdapter() {
return(new FoodsListSliderAdapter(getChildFragmentManager()));
}
上课:
FoodsListSliderAdapter
public class FoodsListSliderAdapter extends FragmentPagerAdapter {
public FoodsListSliderAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int fragmentId) {
switch (fragmentId) {
case 0:
return FoodListPartOne.newInstance(0);
case 1:
return FoodListPartTwo.newInstance(1);
case 2:
return FoodListPartThree.newInstance(2);
}
return FoodListPartOne.newInstance(0);
}
@Override
public int getCount() {
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
if (object instanceof FoodListPartOne) {
view.setTag(0);
}
if (object instanceof FoodListPartTwo) {
view.setTag(1);
}
if (object instanceof FoodListPartThree) {
view.setTag(2);
}
return super.isViewFromObject(view, object);
}
}
必须处理我的代码并将片段附加到viewPager