【发布时间】: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