1

I successfully created TouchImageView Inside Viewpager after this I tried to make a custom TuochImageView with TextView at bottom using single row in pageradapter but unable to inflate ... For this purpose,I am using the following codes to create TouchImageView in android within View pager>>>

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);
    }
}    

I have changed the following in instantiateItem method to :

 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;
}
SID --- Choke_de_Code
  • 1,131
  • 1
  • 16
  • 41
  • For this purpose you are using TouchImageView at first place inside a viewpager but then you tried with TouchImageView with a Textview inside that ViewPager then you had a problem?? @Santosh Bhandary – SID --- Choke_de_Code Apr 08 '15 at 07:58
  • Yes that is the problem @sid_dude ,I tried to do so by making single row xml layout with i)TouchImageView and ii)TextView with in RelativeLayout ,I could not find the proper way to resolve TouchImageView with textView as caption is not comming ... –  Apr 08 '15 at 08:02
  • have you try fragment pager adapter instead pager adapter? – Ajay Pandya Apr 08 '15 at 11:33
  • I don't know the concept of FragmentPagerAdapter ,if you can solve it by this process then please provide me with the codes ,Thanks in advance @Ajay Pandya –  Apr 08 '15 at 12:37
  • actual right now i don't try with your case but most of the time when i'm face this type of problem with pager adapter simply replace the extends PagerAdapter with extends FragmentPagerAdapter or FragmentStatePagerAdapter – – Ajay Pandya Apr 08 '15 at 12:52

1 Answers1

0

You forgot to add view to the container. Add this ((ViewPager) container).addView(view); in the end of instantiateItem before return statement.

The following is my sample code:

    @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;
    }
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
Yuchao Zhou
  • 1,062
  • 14
  • 19