Skip to content Skip to sidebar Skip to footer

Android Viewpager Won't Show Second Page?

I'm using a ViewPager to swipe just part of a screen (as opposed to the whole View/Page). I can hack isViewFromObject to just return true and then my first image appears but my sec

Solution 1:

The PagerAdapter's instantiateItem() method is called for each page, so only one page/child should be inserted into the container.

@Overridepublic Object instantiateItem(ViewGroup container, int position) {
        finalViewpage= insertPhoto("http:" + mImageURLArraylist.get(position) );
        container.addView(page); 
        return page;
    }

    @OverridepublicbooleanisViewFromObject(View view, Object obj) {
        returnview== obj;
    }

Solution 2:

You need to add the view to the container. Try changing your method as shown here:

@Overridepublic Object instantiateItem(ViewGroup container, int position) {
        LayoutInflaterinflater= (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        int RLayoutId;
        RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayoutViewGroupimageLayout= (ViewGroup) inflater.inflate(RLayoutId, container, false);

        container.addView(insertPhoto(imageLayout, "http:" + mImageURLArraylist.get(position) ));
        return imageLayout;

    }

// this needs to be refactored, too many viewgroups. Move all layouts to XMLpublic View insertPhoto(ViewGroup root, String path){
    LinearLayoutlayout=newLinearLayout(getActivity());
    layout.setGravity(Gravity.CENTER);
    ImageViewimageView=newImageView(getActivity());
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
    layout.addView(imageView);
    root.addView(layout);
    return root;        
}

It would also be better if your layout was all done in XML, so the insertPhoto function simply calls Picasso to load the image.

Post a Comment for "Android Viewpager Won't Show Second Page?"