0
首先我沒有得到任何錯誤。我的問題是我在ViewPager中看不到頁面(012)。讓我來解釋:Viewpager內存不足?
首先我直接添加里面的佈置頁面,而無需片段:
<android.support.v4.view.ViewPager
android:id="@+id/drawerPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/page_start_one"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
//3 textviews, 1 imageview
</RelativeLayout>
<RelativeLayout
android:id="@+id/page_start_two"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
//1 textview, 1 imageview
</RelativeLayout>
<RelativeLayout
android:id="@+id/page_start_three"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
//1 textview, 1 imageview
</RelativeLayout>
<RelativeLayout
android:id="@+id/page_start_four"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
//3 textviews, 1 button
</RelativeLayout>
</android.support.v4.view.ViewPager>
我爲簡單起見這樣做。現在我加載尋呼機適配器活動:
WizardPagerAdapter adapter = new WizardPagerAdapter();
ViewPager pager = (ViewPager) findViewById(R.id.drawerPager);
pager.setAdapter(adapter);
當然定義WizardPagerAdapter
。您將在下面看到,destroyItem()
什麼都不做,instantiateItem()
只是返回與id找到的視圖。這是我從this SO question基於代碼:
class WizardPagerAdapter extends PagerAdapter {
public RelativeLayout instantiateItem(View collection, int position) {
System.out.println(position);
int resId = 0;
switch (position) {
case 0:
resId = R.id.page_start_one;
break;
case 1:
resId = R.id.page_start_two;
break;
case 2:
resId = R.id.page_start_three;
break;
case 3:
resId = R.id.page_start_four;
break;
}
return (RelativeLayout) findViewById(resId);
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
}
@Override
public int getCount() {
return 4;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
正如我以前說過,這個代碼,我只能看到0 & 1頁相當於RelativeLayouts但2頁& 3總是空的。
- 你知道爲什麼會發生這種情況嗎?
- 是否因爲內存限制?
- 或者我的另一個假設是頁面2 & 3從來沒有被誇大,因爲onCreate
instantiateItem()
只運行前兩個。 (由System.out.println(position);
嗤之以鼻,打印0 & 1)。