Thursday, 15 August 2013

First viewpager page blank

First viewpager page blank

I have a problem that relates to Android's ViewPager. I am currently
attempting to implement a VerticalViewPager (though that is not relevant
to the problem as this same problem occurs with a normal ViewPager) with a
custom PagerAdapter that gets stuff from my database and inputs it onto
the screen.
The problem is that my first card appears to be blank until I slide it
over to the next page and come back. After that the content is there. I'm
not exactly sure what was going on so hopefully someone can shed some
light on this issue. I found a similar problem here: Why is the first view
in ViewPager appearing empty? and Activity using ViewPager, PagerAdapter
and AsyncTask causes a blank view but it seems as if nothing in there is
able to help me.
This is my custom PagerAdapter code:
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;
}
}

No comments:

Post a Comment