在ListView底部添加进度条

6
我正在创建一个带有无限滚动效果的简单列表视图。 当我下载新数据时,我想在列表视图底部显示一个不确定进度条(类似于Gmail应用)。
有两种解决方案:
1. 将进度条作为列表视图的一项添加,但我不喜欢这个解决方案。
2. 在列表视图底部添加进度条,并显示/隐藏它。
最终效果将类似于这样:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/List_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/trobber"
        android:layout_below="@+id/List_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true" >
    </ProgressBar>

</RelativeLayout>

但是当我的列表长度超过屏幕大小时(也就是说需要滚动),进度条就不会显示。

2个回答

7

使用 yourListView.addFooterView 方法,将进度条视图添加为页脚视图。


谢谢,你节省了我很多时间! - loonis
4
当我在ListView中添加FooterView后,当我尝试为ListView设置适配器时,出现了类强制转换异常。 - codeRider
你有这个问题的解决方案吗? - Harry Sharma
@codeRider 这是在旧版本的安卓系统中出现的问题,例如 JellyBean。 - Rahul Rastogi

2
为了更详细地说明,以下是操作步骤:
A. 创建一个布局xml文件,并将其命名为progress_bar_footer.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <ProgressBar
        android:layout_width="30dip"
        android:layout_height="30dip"
        android:layout_gravity="center"
        android:indeterminateTint="@android:color/holo_green_light"
        android:id="@+id/progressBar5"/>

</LinearLayout>

B. In your activity,

View mProgressBarFooter = ((LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.progress_bar_footer, null, false);

C. 使用它。

listView.addFooterView(mProgressBarFooter); //or
listView.removeFooterView(mProgressBarFooter);

感谢这位John Moses的贡献。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接