我正在尝试构建一个以纵向和横向模式显示图书的应用。很明显,有1页以肖像方式显示,2页以横向方式显示。每个模式都工作得很好但是当我将方向从纵向更改为横向时,viewPager尝试从纵向模式获取2页而不是尝试从横向模式获取2个双页... 实际上 FragmentStatePagerAdapter会保留以纵向方式创建的2个片段,并在为横向模式创建2个双页之前使用它们。如果我再次以纵向模式切换,FragmentStatePagerAdapter使用以前在横向模式下创建的2个片段,所以我再次看到1个双页而不是1个单页等。如果我继续切换方向,我得到一个OutOfMemoryError,因为FragmentStatePagerAdapter永远不会在方向改变时冲洗它的碎片。
以下是2个用例,以便于理解:
我向右滑动,我看到第5页和第6页确定
我以纵向模式启动viewPager
我向左滑动,我看到第2页和第3页确定
public class PlayerFragmentActivity extends FragmentActivity {
private Intent mIntent;
private ViewPager mPortraitPager;
private ViewPager mLandscapePager;
private Boolean mIsLandscape;
private String mKey;
private int mNbPages;
private int mNbDoublePages;
private PageFactory mPageFactory;
private DoublePageFactory mDoublePageFactory;
private PagerAdapter mPagerAdapter;
@Override
protected void onStop(){
super.onStop();
mPagerAdapter = null;
mDoublePageFactory = null;
mPageFactory = null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_player);
mIntent = getIntent();
mKey = mIntent.getStringExtra("key");
mNbPages = mIntent.getIntExtra("nbPages", 0);
mNbDoublePages = DoublePageFactory.getDoublePageNumFromPageNum(mNbPages);
Resources res = getResources();
mIsLandscape = (res.getConfiguration().orientation == 1) ? false : true;
mPortraitPager = (ViewPager) findViewById(R.id.portraitPager);
mLandscapePager = (ViewPager) findViewById(R.id.landscapePager);
mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
if (mIsLandscape) {
mDoublePageFactory = new DoublePageFactory(this, mKey, mNbPages, res.getInteger(R.integer.nb_page_columns), res.getInteger(R.integer.nb_page_columns));
mPortraitPager.setVisibility(View.GONE);
mLandscapePager.setAdapter(mPagerAdapter);
mPortraitPager.setAdapter(null);
} else {
mPageFactory = new PageFactory(this, mKey, this.mNbPages, res.getInteger(R.integer.nb_page_columns), res.getInteger(R.integer.nb_page_columns));
mLandscapePager.setVisibility(View.GONE);
mPortraitPager.setAdapter(mPagerAdapter);
mLandscapePager.setAdapter(null);
}
}
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
if(mIsLandscape){
Fragment doublePageFragment = new DoublePageFragment();
args.putInt(DoublePageFragment.ARG_DOUBLEPAGE_NUM, position + 1);
doublePageFragment.setArguments(args);
return doublePageFragment;
}else{
Fragment pageFragment = new PageFragment();
args.putInt(PageFragment.ARG_PAGE_NUM, position + 1);
pageFragment.setArguments(args);
return pageFragment;
}
}
@Override
public int getCount() {
return (mIsLandscape) ? mNbDoublePages:mNbPages;
}
/* j'ai essayé cette méthode mais ça ne fonctionne pas :( */
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
super.destroyItem(container, position, object);
}
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
return "p." + position + 1;
}
}
public boolean isLandscape() {
return mIsLandscape;
}
public ImageView getSinglePage(int position) {
return mPageFactory.getPage(position);
}
public LinearLayout getDoublePage(int position) {
return mDoublePageFactory.getDoublePage(position);
}
}
public class PageFragment extends Fragment {
private PlayerFragmentActivity mPlayerFragmentActivity;
public static final String ARG_PAGE_NUM = "page_number";
public static final String ARG_WOBOOK_DIRECTORY = "book_directory";
public static final String ARG_NB_PAGE_COLUMNS = "nb_page_columns";
public static final String ARG_NB_PAGE_ROWS = "nb_page_rows";
@Override
public ImageView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mPlayerFragmentActivity = ((PlayerFragmentActivity) getActivity());
return mPlayerFragmentActivity.getSinglePage(getArguments().getInt(ARG_PAGE_NUM));
}
@Override
public void onStop(){
super.onStop();
mPlayerFragmentActivity = null;
}
}
public class DoublePageFragment extends Fragment {
private PlayerFragmentActivity mPlayerFragmentActivity;
public static final String ARG_DOUBLEPAGE_NUM = "double_page_number";
public static final String ARG_WOBOOK_DIRECTORY = "book_directory";
public static final String ARG_NB_PAGE_COLUMNS = "nb_page_columns";
public static final String ARG_NB_PAGE_ROWS = "nb_page_rows";
@Override
public LinearLayout onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mPlayerFragmentActivity = ((PlayerFragmentActivity) getActivity());
return mPlayerFragmentActivity.getDoublePage(getArguments().getInt(ARG_DOUBLEPAGE_NUM));
}
@Override
public void onStop(){
super.onStop();
mPlayerFragmentActivity = null;
}
}
我已经读过框架中的FragmentManager负责保存状态并恢复寻呼机所做的任何活动片段。所以我想当方向改变时,FragmentManager会说
嘿!我已经创建了2个项目,所以我不需要创建新闻, 让我们来吧
但实际上它从旧的定位模式获得了2个项目(页面/双页)。因此,当发生方向更改时,我需要找到一种方法来删除FragmentManager保存的片段。
我也测试了this method。问题似乎与this one
有关祝你好运
答案 0 :(得分:-1)
尝试将方向限制为纵向或横向模式。因为我们只能在精确模式下使用视图寻呼机。