How to add a Progressbar to a Pageradapter?

Asked

Viewed 87 times

1

I have a Viewpager that uses a Pageradapter to make an image slide. In my class extending Pageradapter I have an Asynctask that loads images from the internet and plays in Imageview. How can I put a Progressbar spinner to keep running while the image does not load?

I tried this way, but does not show the Progressbar on the screen:

...

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

linearLayout.addView(progressBar);
...

1 answer

1


I was able to solve it. In my class, Pageradapter, 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 the Progressbar on Asynctask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.