0
目前,我的ViewPager工作是這樣的:如何防止Viewpager在旋轉後變爲空白?
但之後,我旋轉設備,我viewpager變成空白,並有兩個箭頭的每一頁,這樣:
這裏是我的活動代碼:
public class MainViewPagerHelper {
private static MainViewPagerHelper sViewPagerHelper = null;
private Activity mActivity;
private MainViewPagerAdapter adapter;
public MainViewPagerHelper(Activity activity) {
this.mActivity = activity;
}
public static MainViewPagerHelper getInstance(Activity activity) {
if (sViewPagerHelper == null) {
sViewPagerHelper = new MainViewPagerHelper(activity);
}
return sViewPagerHelper;
}
public void dateBarFunction() throws ParseException {
mDateAreaSlider = (ViewPager) mActivity.findViewById(R.id.date_area);
adapter = new MainViewPagerAdapter(mActivity, getDateList());
mDateAreaSlider.setAdapter(adapter);
mDateAreaSlider.setCurrentItem(adapter.DateList.length - 1);
mDateAreaSlider.setOffscreenPageLimit(2);
mButtonBack = (ImageView) mActivity.findViewById(R.id.btn_back_main);
mButtonNext = (ImageView) mActivity.findViewById(R.id.btn_next_main);
// Hide right arrow if reach last position at first launch
if (mDateAreaSlider.getCurrentItem() == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
}
// Hide left arrow if reach first position at first launch
if (mDateAreaSlider.getCurrentItem() == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
}
// Hide both arrows if there is just 1 date
if (adapter.getCount() == 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.INVISIBLE);
}
slideToDrawMap();
}
public void slideToDrawMap() {
final ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// Draw map
String date = adapter.DateList[position].getDate();
try {
// Scroll to item in timeline
mTimeLineHelper = TimeLineHelper.getInstance(mActivity);
mTimeLineHelper.scrollToDateSelected(date);
mDrawMap.drawStayPointAndRoute(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (position == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}
// Hide left arrow if reach first position
else if (position == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
mDateAreaSlider.addOnPageChangeListener(onPageChangeListener);
}
這裏是我的適配器:
public class MainViewPagerAdapter extends PagerAdapter {
public CalendarModel[] DateList;
private Context mContext;
private LayoutInflater mInflater;
private RelativeLayout mDateAreaLayoutInflated;
public MainViewPagerAdapter(Context mContext, CalendarModel[] mDateList) {
this.mContext = mContext;
this.DateList = mDateList;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
//Just instantiate items,nothing special here
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
@Override
public int getCount() {
return DateList.length;
}
請告訴我這是爲什麼發生,如何解決this.Thank你!
對不起,但第一個代碼示例不是活動。 –
這只是一個幫手類,你可以看到我把我的活動傳遞給它;)。在我的活動中,我只是調用方法dateBarFunction() –
嘗試將此行添加到清單中的活動標記:android:configChanges =「orientation | screenSize」' – KrisPrajapati