2

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;
    }
}
csm232s
  • 1,660
  • 4
  • 32
  • 60
  • 6
    it is all becuase `mAvatarView` is a PagerAdapter member ...get rid of `private CircleImageView mAvatarView;` and use `final CircleImageView mAvatarView` instead (have to be final to pass it to anonymous interface implementation, have to be local to avoid messing with variable in next instantiateItem call) ... seems like you have a problems with basic code flow in java – Selvin Jan 14 '15 at 22:45
  • 4
    `final CircleImageView mAvatarView` inside `instantiateItem` to be more precise – Selvin Jan 14 '15 at 22:51
  • @Selvin, perfect! If you'd like to create this as the answer I'll mark it appropriately. – csm232s Jan 14 '15 at 22:59
  • Perfect. Solved my issue as well – Rashid Afzaal Apr 21 '20 at 13:54

0 Answers0