1

この後、Viewpager 内に 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 に答える