问题
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;
}
}
回答1:
I had the same issue with ViewPagers, I used them with Fragments. The fragment inside first page was always empty second and later pages were fine and first pages was ok when i returned from 2nd or 3rd page.
I found out the reason was that I loaded the data from database in onCreateView method of the fragment, Views were being created before data could be loaded inside them from database. I changed my code to load data in onStart method of the fragment and everything was fine.
@Override
public void onStart() {
getActivity().getLoaderManager().initLoader(LOADER_ID,null, this);
super.onStart();
}
Hope this helps.
回答2:
The issue must be because card is taking time to load data from database and view is not updated after data is 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 so that view is updated after 1 second when it is being created.mPosition
is the postion of the first card that is being opened.If you are opening viewpager by clicking on item in list then you need to pass the postion of the item that is being selected when calling adapter..else u can set mPostion
as 0 if the first item is always initially opened in viewpager
来源:https://stackoverflow.com/questions/18265384/first-viewpager-page-blank