android - First viewpager page blank -
i have problem relates android's viewpager. attempting implement verticalviewpager (though not relevant problem same problem occurs normal viewpager) custom pageradapter gets stuff database , inputs onto screen.
the problem first card appears blank until slide on next page , come back. after content there. i'm not sure going on can shed light on issue. found similar problem here: why first view in viewpager appearing empty? , activity using viewpager, pageradapter , asynctask causes blank view seems if nothing in there able me.
this 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; } }
the issue must because card taking time load data database , view not updated after data being loaded.try adding
handler.postdelayed(r=new runnable() { @override public void run() { if (mposition == position) { notifydatasetchanged(); handler.removecallbacks(r); } } }, 1000);
inside instantiateitem
of adapter view updated after 1 second when being created.mposition
postion of first card being opened.if opening viewpager clicking on item in list need pass postion of item being selected when calling adapter..else u can set mpostion
0 if first item opened in viewpager
Comments
Post a Comment