[Solved] Implementing ViewPager for displaying the images that comes from server


First you need a custom viewpager adapter :
Picasso is a great library to load images from. I will give you a link in case you need further help understanding it : http://square.github.io/picasso/

public class ViewPagerAdapter extends PagerAdapter {
Context c;
private List<String> _imagePaths;
private LayoutInflater inflater;

public ViewPagerAdapter(Context c, List<String> imagePaths) {
    this._imagePaths = imagePaths;
    this.c = c;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imgDisplay;

    inflater = (LayoutInflater) c
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.pager_item, container,
            false);

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);

    Picasso.with(c).load(_imagePaths.get(position)).into(imgDisplay);
    (container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    (container).removeView((RelativeLayout) object);

}
}

This is the pager_item.xml :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

From your activity:

After fetching the list urls from Rest : This is what you do :

List<String> urls;

public class MainPage extends Activity {
 public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.main_page);
urls= new ArrayList<>();
    pager = (ViewPager) findViewById(R.id.pager);
urls.add("www.image1.com");
urls.add("www.image2.com");
        pager.setAdapter(new ViewPagerAdapter(getApplicationContext(), urls));

}
}

12

solved Implementing ViewPager for displaying the images that comes from server