问题
I am new to android development and I am attempting to create a ViewPager
with a custom PagerAdapter
. In my page adapter layout, I have an CircleImageView and I'm using Ion with a overridden Future callback to download and set the image.
However, when the activity loads, the first page has an empty ImageView
(the TextViews
contain the correct information). When I scroll to the second page, I see the first page's image. This is a continuous cycle until the view is destroyed and I scroll back to it again (at which time the correct image is now showing). Just to ensure it wasn't an issue with CircleImageView
, I used the standard ImageView
and experienced the same issue.
public class UserPagerAdapter extends PagerAdapter {
private ArrayList<User> mUsers;
private CircleImageView mAvatarView;
public UserPagerAdapter(ArrayList<User> mUsers, Context context) {
super();
this.mUsers = mUsers;
}
@Override
public int getCount() {
return mUsers.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page = inflater.inflate(R.layout.user_page, null);
TextView firstName = (TextView) page.findViewById(R.id.txtName);
TextView email = (TextView) page.findViewById(R.id.txtEmail);
User user = mUsers.get(position);
mAvatarView = (CircleImageView) page.findViewById(R.id.imgAvatar);
firstName.setText(user.firstName + " " + user.lastName);
email.setText(user.email);
Ion.with(page.getContext())
.load(user.gravatar)
.asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
mAvatarView.setImageBitmap(result);
}
});
container.addView(page, 0);
return page;
}
}
来源:https://stackoverflow.com/questions/27953784/image-not-loading-in-first-page-of-viewpager-pageradapter