viewrager with pagerAdapter不更新其视图

时间:2017-11-14 12:12:57

标签: android-viewpager android-pageradapter

当用户选择图像时,我在视图寻呼机内部有一个imageview我需要在第一个位置的视图寻呼机中显示最近的图像,但是它的viewpager没有用最近的图像更新。 我的代码:

public static class CustomPagerAdapter extends PagerAdapter {

    Context mContext;
    LayoutInflater mLayoutInflater;
    Bitmap newBitmapUploadedImage = null;

    public CustomPagerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public CustomPagerAdapter(Context context, Bitmap newUploadedImage) {
        mContext = context;
        newBitmapUploadedImage = newUploadedImage;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        int swapPicSize = 0;
        if (newBitmapUploadedImage == null) {
            swapPicSize = Singleton.galleryPicsData.size();
        } else {
            swapPicSize = Singleton.galleryPicsData.size() + 1;
        }

        return swapPicSize;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View itemView = mLayoutInflater.inflate(R.layout.view_pager_item, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(iv_user_pics);
        // imageView.setImageResource(profileViewPagerPics[position]);
       /* DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) imageView.getLayoutParams();
        params.width = metrics.widthPixels;
        params.leftMargin = 0;
        imageView.setLayoutParams(params);
        getActivity().getWindow().setFormat(PixelFormat.TRANSPARENT);*/
        if (Singleton.galleryPicsData.size() == 0) {
            if (Singleton.getUserProfileInfoModel().getGender()) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.male_user_profile_photo));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.female_user_profile_photo));

            }

        }
        if (newBitmapUploadedImage != null) {
            imageView.setImageBitmap(newBitmapUploadedImage);
        } else {
            GalleryPics gallery_pics = Singleton.galleryPicsData.get(position);
            Picasso.with(mContext).load(ServerUtilities.BASE_IMAGE_URL + gallery_pics.getPicPath()).into(imageView);
            Log.d("user_galler_pic_img_url", "" + ServerUtilities.BASE_IMAGE_URL + Singleton.galleryPicsData.get(position).getPicPath());
            userProfilePicsProgressBar.setVisibility(View.GONE);
            itemView.setTag(gallery_pics);
            container.addView(itemView);
            notifyDataSetChanged();
        }
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((LinearLayout) object);
    }



    @Override
    public int getItemPosition(Object object) {
        GalleryPics galleryPic = (GalleryPics) ((View) object).getTag();
        int position = Singleton.galleryPicsData.indexOf(galleryPic);
        if (position >= 0) {
            // The current data matches the data in this active fragment, so let it be as it is.
            return position;
        } else {
            // Returning POSITION_NONE means the current data does not matches the data this fragment is showing right now.  Returning POSITION_NONE constant will force the fragment to redraw its view layout all over again and show new data.
            return POSITION_NONE;
        }
    }
}

我尝试了上面的代码,但没有用,仍然是我的旧图像,你能建议我如何更新这个viewpager

1 个答案:

答案 0 :(得分:0)

如果您看到图片网址,则每次更新新图片时,服务器都会提供相同的网址(没有更改),但可能会有所不同。这意味着首先上传一张图片,然后获取第一张图片网址,然后再上传第二张图片然后获取第二个URL,只需比较两个URL。如果两者都相同,Picasso不会更新它,而是保持冷静。所以使用加载位图的Native方法并在ImageView中显示它,它肯定会更新它。