【问题标题】:viewpager with pagerAdapter not updating its views带有 pagerAdapter 的 viewpager 不更新其视图
【发布时间】:2017-11-14 12:12:56
【问题描述】:

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

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

【问题讨论】:

    标签: android-viewpager android-pageradapter


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多