如何在 Android 中向 ViewPager 添加进度条

How to Add progress bar to ViewPager in Android(如何在 Android 中向 ViewPager 添加进度条)
本文介绍了如何在 Android 中向 ViewPager 添加进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Android 中为 ViewPager 添加进度条?

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

我的情况是我必须在 ViewPager 中加载 imageURL.当用户滑动页面时,如果 ImageURL 没有加载,我需要显示进度条/正在加载 GIF 图片.

My scenario is that I have to load imageURL's in a ViewPager. When the user swipes the pager, if the ImageURL is not loaded I need to show the progress bar/loading GIF image.

有可能吗?

推荐答案

我认为 Satya 正在考虑以下类似的事情.在您的 viewpager 适配器类中,您应该有这个方法:

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;
}

还有像这样的 R.layout.fotopager_buttons.xml:

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

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true" />

<ProgressBar
    android:id="@+id/loading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

您可以在此处查看 imageLoader 类的其余部分:https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/UniversalImageLoaderExample/src/com/nostra13/example/universalimageloader/ImagePagerActivity.java

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

这篇关于如何在 Android 中向 ViewPager 添加进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)