我正在使用viewpager里面的片段。我正在使用pagerAdapter通过XML在两个页面之间切换。这是代码:
public class DemoPagerAdapter extends FragmentPagerAdapter {
public DemoPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
Fragment f;
switch (index) {
case 0:
f = new FirstFragment();
break;
case 1:
f = new SecondFragment();
break;
}
return f;
}
@Override
public int getCount() {
return 2;
}
}
我这样称呼它:
public class DemoFragment1 extends Fragment {
private ViewPager viewPager;
private DemoPagerAdapter mAdapter;
public DemoFragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.security, container, false);
mAdapter = new DemoPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
showPages(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
return rootView;
}
一切正常。但如果我转到DemoFragment2然后回到DemoFragment1,则不会调用PagerAdapter。
答案 0 :(得分:3)
您的ViewPager嵌套在Fragment中,您应该使用ChildFragmentManager,修改:
mAdapter = new DemoPagerAdapter(getChildFragmentManager());
我还假设ViewPager会将屏幕外片段保留在内存中,因此可能无法再次调用getItem(int index)
。添加
viewPager.setOffscreenPageLimit(0);
return rootView;
答案 1 :(得分:0)
好吧,上周我对这个问题感到满意。
当我将ViewPager
放在带有FragmentPagerAdapter
的片段中时,在我向前移动到片段并返回之后,寻呼机的视图不存在。
我认为FragmentPagerAdapter
不会在ViewPager
的主机片段中保留其视图,它们的生命周期不一样。虽然你把VewPager
的主机片段放在后面的堆栈中,但FragmentPagerAdapter
的片段没有。然后,我放弃FragmentPagerAdapter
并尝试android.support.v4.view.PagerAdapter
,(注意,它不会使用片段来注入视图!),一切都很好!
下面是样本:
private final PagerAdapter coverPager = new PagerAdapter() {
private LayoutInflater mLayoutInflater;
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (mLayoutInflater == null) {
mLayoutInflater = LayoutInflater.from(getActivity());
}
switch (position) {
case 0:
View frontPage = mLayoutInflater.inflate(R.layout.profile_cover, container, false);
ImageView avatar = (ImageView) frontPage.findViewById(R.id.avatar);
Picasso.with(getActivity())
.load(mAvatarUrl)
.placeholder(R.drawable.error)
.error(R.drawable.error)
.into(avatar);
TextView screenName = (TextView) frontPage.findViewById(R.id.screen_name);
screenName.setText("@" + mScreenName);
TextView remark = (TextView) frontPage.findViewById(R.id.remark);
remark.setText(TextUtils.isEmpty(mRemark) ? mScreenName : mRemark);
if (mVerified) {
frontPage.findViewById(R.id.verified).setVisibility(View.VISIBLE);
}
container.addView(frontPage);
return frontPage;
case 1:
View introPage = mLayoutInflater.inflate(R.layout.profile_intro, container, false);
CatnutUtils.setText(introPage, R.id.description, TextUtils.isEmpty(mDescription)
? getString(R.string.no_description) : mDescription);
CatnutUtils.setText(introPage, R.id.location, mLocation);
CatnutUtils.setText(introPage, R.id.profile_url, Constants.WEIBO_DOMAIN + mProfileUrl);
container.addView(introPage);
return introPage;
default:
return null;
}
}
};
然后在onViewCreate
或OnCreateView
中,添加mViewPager.setAdapter(coverPager);
instantiateItem
是放置网页视图的地方,类似于FragmentPagerAdapter
!
就像把FragmentAdapter
的视图放在ViewPager
的主机片段中,而不是另一个片段!
希望它有用!