我有一個與Android的ViewPager相關的問題。我目前正在試圖實現一個VerticalViewPager(儘管這與問題無關,因爲與普通ViewPager相同的問題)與自定義的PagerAdapter從我的數據庫中獲取內容並將其輸入到屏幕上。第一個視圖頁面空白
問題是我的第一張牌看起來是空白的,直到我將它滑到下一頁然後回來。之後,內容就在那裏。我不確定發生了什麼,所以希望有人能夠在這個問題上提出一些看法。我在這裏發現了一個類似的問題: Why is the first view in ViewPager appearing empty? 和Activity using ViewPager, PagerAdapter and AsyncTask causes a blank view但似乎沒有任何內容能夠幫助我。
這是我的自定義代碼PagerAdapter:
public class BeginnerCardCursorPagerAdapter extends PagerAdapter {
private Cursor cursor;
private LayoutInflater inflater;
public BeginnerCardCursorPagerAdapter(Context context, Cursor cursor){
this.cursor = cursor;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void swapCursor(Cursor cursor){
this.cursor = cursor;
}
@Override
public void destroyItem(View view, int position, Object object) {
((VerticalViewPager) view).removeView((RelativeLayout) object);
}
@Override
public int getCount() {
if(cursor == null) {
return 0;
} else {
return 90;
}
}
@Override
public Object instantiateItem(View view, int position) {
RelativeLayout layout = null;
int position2 = position;
if(position == 0){
layout = (RelativeLayout) inflater.inflate(R.layout.activity_slide_info, null);
}if(position == 89){
layout = (RelativeLayout) inflater.inflate(R.layout.activity_advertisement_beginner, null);
}
else if(position > 0 && position < 89){
position2--;
cursor.moveToPosition(position2);
layout = (RelativeLayout) inflater.inflate(R.layout.activity_card, null);
layout.setBackgroundResource(R.drawable.card_beginner);
TextView cardTitle = (TextView) layout.findViewById(R.id.pirate_card_title);
TextView cardExample = (TextView) layout.findViewById(R.id.pirate_card_example);
TextView cardDefinition = (TextView) layout.findViewById(R.id.pirate_card_definition);
cardTitle.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_TITLE)));
cardExample.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_SENTENCE)));
cardDefinition.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_DEFINITION)));
}
((VerticalViewPager) view).addView(layout);
return layout;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
刪除此行((VerticalViewPager)視圖).removeView((RelativeLayout)對象);來自destroyitem方法。 –
不幸的是,沒有骰子 – user2525981
你使用動態高度viewpager嗎? – Govind