0

I want to implement one code where I need to add progress bar on ViewPager, for every pager progress bar will show until image will not load. for more details you can check this link Link There is a ViewPager where images is coming from server side which takes time to load I want to add progress bar until image will not load

This is layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >

<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:layout_gravity="right|top" />

<ProgressBar android:id="@+id/progressBar"
    android:progressDrawable="@drawable/loader_image"
    android:layout_width="fill_parent" android:layout_height="8dip"
    style="?android:attr/progressBarStyleHorizontal"
    android:indeterminateOnly="false"
    android:max="100">
</ProgressBar>

public class SlidingImage_Adapter extends PagerAdapter {
private ArrayList<String> IMAGES;
private LayoutInflater layoutInflater;
private Context context;
ProgressBar progressBar;
private ImageLoader imageLoader;

public SlidingImage_Adapter(Context context, ArrayList<String> IMAGES) {
    this.context = context;
    this.IMAGES=IMAGES;
    for(int i=0;i<IMAGES.size();i++)
        layoutInflater = LayoutInflater.from(context);
}


@Override
public Object instantiateItem(ViewGroup view, int position) {
  //  ImageView imageLayout = (ImageView) inflater.inflate(R.layout.screen_slide_fragment, view, false);
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view = layoutInflater.inflate(R.layout.screen_slide_fragment, view, false);
    ImageView imageView = (ImageView) item_view.findViewById(R.id.image);
    Log.e("instantiateItem: ",IMAGES.get(position) );
    Picasso.with(context)
            .load(IMAGES.get(position))
            .placeholder(R.drawable.loader_image)
            .into(imageView);

    return item_view;
}

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

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


@Override
public boolean isViewFromObject(View view, Object object) {
    return view.equals(object);

}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}

@Override
public Parcelable saveState(){
    return null;
   }
 }

thank you

Vikas Godiyal
  • 117
  • 3
  • 15

1 Answers1

1

Use Picasso to load the image that comes with loader settings

Picasso.with(context)
        .load(imageUrl)
        .fit()
        .error(R.drawable.error_drawable)
        .placeholder(R.drawable.loader_image)
        .into(imageView);

screen_slide_fragment.xml should contain only ImageView as the rootView. This is how your method would look like

@Override
public Object instantiateItem(ViewGroup view, int position) {
    ImageView imageLayout = (ImageView) inflater.inflate(R.layout.screen_slide_fragment, view, false);

    Picasso.with(context)
        .load(IMAGES.get(position))
        .placeholder(R.drawable.loader_image)
        .into(imageLayout);

    return imageLayout;
}

Add this in your app's gradle file

compile 'com.squareup.picasso:picasso:2.5.2'
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60