我正在使用一个ViewPager
包含多个 ListViews 的代码,其代码类似于Infinite ViewPager的答案。这个想法是为谷歌日历应用程序提供类似于日视图的东西(其来源似乎不可用;只有默认日历应用程序是,但它使用 a ViewSwitcher
) - 我想让它看起来像用户可以无限左右滑动,但实际上只有 3 个项目ViewPager
,当用户点击页面 0 或 2 时,我们将 1 设置为当前页面并相应更新。
现在,这一切都奏效了。然而,奇怪的是,当手机旋转并重建 Activity 时(我暂时避免使用configChanges
),应用程序中的页面再次实例化,但无序。顺序不是 0->1->2,而是 1->0->2,这会打乱应用程序中页面的顺序。
我的片段,在onActivityCreated()
:
mPagerAdapter = new ContinuousPagerAdapter(R.layout.my_listview, this);
// set the adapter
mViewPager = (ViewPager) getView().findViewById(R.id.agendaViewPager);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setOnPageChangeListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setSaveEnabled(false);
// ...
mViewPager.setCurrentItem(1, false);
loadData();
寻呼机适配器:
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
public class ContinuousPagerAdapter extends PagerAdapter {
OnPageInstantiatedListener pListener;
ViewPager container;
int childLayoutResId;
@SuppressWarnings("unused")
private ContinuousPagerAdapter() {
}
/**
* @param childLayoutResId Layout resource ID of the children to be inflated
*/
public ContinuousPagerAdapter(int childLayoutResId, OnPageInstantiatedListener pListener) {
this.childLayoutResId = childLayoutResId;
this.pListener = pListener;
}
@Override
public int getCount() {
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(View container, int position) {
this.container = (ViewPager) container;
// inflate a new child view
LayoutInflater li = (LayoutInflater) container.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View childView = li.inflate(childLayoutResId, null, false);
// add it to the view pager and return
int count = this.container.getChildCount();
int actualPos = count > position ? position : count;
this.container.addView(childView, actualPos);
pListener.onPageInstantiated(actualPos); // sometimes use 0 instead of actualPos, with different but still inconsistent results
return childView;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeViewAt(position);
}
public static interface OnPageInstantiatedListener {
public void onPageInstantiated(int position);
}
/**
* Needed to ensure all the items are instantiated
*/
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public void finishUpdate(View container) {
}
}
我不明白为什么页面在旋转后以 1->0->2 的顺序实例化。我也没有保存状态。对此的任何见解都会有所帮助。