当用户通过图库选择一个图像时,我在视图分页器中有一个图像视图,我需要在视图分页器的第一个位置显示最近的图像,但它的视图分页器没有更新最近的图像。我的代码:
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
发布于 2017-11-28 19:07:08
如果你看到你的图片网址,每次你更新新的图片,服务器会提供相同的网址(没有变化),但它可能是different.That意味着首先上传一张图片并获得第一张图片网址然后上传第二张图片,然后获得第二个网址,只是比较两个网址。如果两者是相同的,毕加索不更新它,而是保持calm.So使用本地方法,即加载位图并显示在ImageView中,它肯定会更新它。
https://stackoverflow.com/questions/47285592
复制相似问题