Image not loading in first page of ViewPager (PagerAdapter)

我怕爱的太早我们不能终老 提交于 2020-01-06 02:28:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!