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.

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

    @Override
    public boolean isViewFromObject (View view, Object obj) {
        return view == obj;
    }

Solution 2:

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

    @Override
    public Object instantiateItem (ViewGroup container, int position) {
        LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        int RLayoutId;
        RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout

        ViewGroup imageLayout = (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 XML
public View insertPhoto(ViewGroup root, String path){
    LinearLayout layout = new LinearLayout(getActivity());
    layout.setGravity(Gravity.CENTER);
    ImageView imageView = new ImageView(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?"