How to Add progress bar to ViewPager in Android

前端 未结 2 1990
抹茶落季
抹茶落季 2021-01-16 09:19

How can I add a progress bar to a ViewPager in Android?

My scenario is that I have to load imageURL\'s in a ViewPager. When the user swipes

2条回答
  •  隐瞒了意图╮
    2021-01-16 09:54

    I think Satya was thinking to something like this in the following. In your viewpager adapter class you should have this method:

    @Override
                public Object instantiateItem(View collection, final int position) {
    
                    LayoutInflater inflater = (LayoutInflater) collection.getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    
                    final FrameLayout imageLayout = (FrameLayout) inflater.inflate(R.layout.fotopager_buttons, null);
                    final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
                    final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
    
                    imageLoader.displayImage(Gallery.get_big_image_url(position), imageView, options, new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted() {
                        spinner.setVisibility(View.VISIBLE);
                        }
    
                        @Override
                        public void onLoadingFailed() {
                        spinner.setVisibility(View.GONE);
                        imageView.setImageResource(android.R.drawable.ic_delete);
                        }
    
                        @Override
                        public void onLoadingComplete() {
                        spinner.setVisibility(View.GONE);
                        }
                        });
    
    
                    views.put(position, iv);
    
                    ((ViewPager) collection).addView(imageLayout,0);
    
                    return imageLayout;
    }
    

    And the R.layout.fotopager_buttons.xml like that:

    
    

    
    
    
    

    You can see the rest of the imageLoader class here: https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/UniversalImageLoaderExample/src/com/nostra13/example/universalimageloader/ImagePagerActivity.java

提交回复
热议问题