1

我在 Viewpager 中成功创建了 TouchImageView 在此之后我尝试使用 pageradapter 中的单行在底部使用 TextView 制作自定义 TuochImageView 但无法膨胀...为此,我使用以下代码在 View pager 中的 android 中创建 TouchImageView> >>

private 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;
}
4

1 回答 1

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;
    }
于 2015-05-19T14:51:19.050 回答