How to add a ProgressBar in a PagerAdapter?

1

I have a ViewPager that uses a PagerAdapter to make an image slide. In my class that extends PagerAdapter I have an AsyncTask that loads the images from the internet and plays in ImageView. How can I put a ProgressBar spinner to stay running while the image does not load?

I tried this way but did not display ProgressBar on the screen:

...

ProgressBar progressBar = new ProgressBar(context, null, 
            android.R.attr.progressBarStyleLarge);
    progressBar.setIndeterminate(true);
    progressBar.setVisibility(View.VISIBLE);

linearLayout.addView(progressBar);
...
    
asked by anonymous 05.04.2016 / 12:46

1 answer

1

I managed to resolve. In my PagerAdapter exiting class, I did the following:

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

    RelativeLayout relativeLayout =  new RelativeLayout(context);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    relativeLayout.setLayoutParams(layoutParams);
    container.addView(relativeLayout);

    ImageView imageView = new ImageView(context);
    imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    relativeLayout.addView(imageView);

    RelativeLayout progressLayout =  new RelativeLayout(context);
    progressLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT));
    progressLayout.setGravity(Gravity.CENTER);
    relativeLayout.addView(progressLayout);


    ProgressBar progressBar = new ProgressBar(context,null,android.R.attr.progressBarStyleLarge);
    progressBar.setVisibility(View.GONE);
    progressBar.setIndeterminate(true);
    progressLayout.addView(progressBar);

    try {
        new ImageLoadTask(imagesUrl[position], imageView , progressBar).execute();
    }catch (Exception e){
        Log.e("Error" , e.getMessage());
    }

    return relativeLayout;
}//instantiateItem

Now just work on ProgressBar in AsyncTask.

    
05.04.2016 / 20:39