图像未在ViewPager(PagerAdapter)的第一页中加载

时间:2015-01-14 22:40:28

标签: android android-viewpager android-pageradapter

我是Android开发的新手,我正在尝试使用自定义ViewPager创建PagerAdapter。在我的页面适配器布局中,我有一个CircleImageView,我使用Ion并重写了Future回调来下载和设置图像。

但是,当活动加载时,第一页的空ImageViewTextViews包含正确的信息)。当我滚动到第二页时,我会看到第一页的图像。这是一个连续循环,直到视图被破坏,然后我再次滚动回来(此时正在显示正确的图像)。为了确保CircleImageView不是问题,我使用了标准ImageView并遇到了同样的问题。

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;
    }
}

0 个答案:

没有答案