我成功创建了TouchImageView Inside Viewpager之后,我尝试使用Pageradapter中的单行在底部使用TextView制作自定义TuochImageView但无法膨胀... 为此,我使用以下代码在View pager>>>
中的android中创建TouchImageViewprivate class ImagePagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return image_name_array.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((TouchImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = getActivity().getApplicationContext();
// define Image View
TouchImageView imageView = new TouchImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
try {
// draw image using bitmap
Bitmap image_drawable = BitmapFactory.decodeStream(getActivity().getAssets().open(image_name_array.get(position)));
imageView.setImageDrawable(new BitmapDrawable(getActivity().getResources(), image_drawable));
} catch (Exception e) {
e.printStackTrace();
}
// imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
我已将instantiateItem方法中的以下内容更改为:
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imageDisplay;
TextView description;
inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.details_images_caption, container, false);
imageDisplay = (TouchImageView) view.findViewById(R.id.gallery_image);
description = (TextView) view.findViewById(R.id.gallery_description);
//Log.e("image aptadapter =>", image_caption.get(position).toString());
description.setText(image_caption.get(position));
String picture = image_name_array.get(position);
Log.e("image DataADA =>", picture);
if (!TextUtils.isEmpty(picture)) {
try {
Bitmap image_drawable = BitmapFactory.decodeStream(context.getAssets().open(picture));
imageDisplay.setImageDrawable(new BitmapDrawable(context.getResources(), image_drawable));
} catch (Exception e) {
}
}
return view;
}
答案 0 :(得分:0)
您忘了向容器添加视图。在return语句之前的((ViewPager) container).addView(view);
末尾添加此instantiateItem
。
以下是我的示例代码:
@Override
public View instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) LoginActivity.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
TouchImageView imageView = (TouchImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(images1[position]);
TextView textView = (TextView) view.findViewById(R.id.screenSlideDescription);
textView.setText("" + position);
((ViewPager) container).addView(view);
return view;
}