活动开始时ProgressBar的进度无法正常工作

时间:2012-12-01 11:48:24

标签: android android-progressbar

我正在使用ExpandableListActivity,在底部我有一个ProgressBar来确定检查了多少行。

活动的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/activity_main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >

    <include
        android:id="@+id/activity_checklist_titlebar"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="10"
        layout="@layout/titlebar" />

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90" >
    </ExpandableListView>
</LinearLayout>

<ProgressBar
    android:id="@+id/activity_checklist_progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_activity_checklist_progress"
    android:layout_alignParentBottom="true" />

<TextView
        android:id="@+id/activity_checklist_progress_text"
        style="@style/Text.Strong.Medium"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/height_activity_checklist_progress"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="0/50" />

</RelativeLayout>

我在这里更新进度:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{
    // ...
    TextView overall_progress_text = 
       (TextView) main_layout.findViewById(R.id.activity_checklist_progress_text);
        int overall_progress = checklist.getProgress();
        overall_progress_text.setText(overall_progress + "/" + checklist.size());
    ProgressBar overall_progress_bar = 
       (ProgressBar) main_layout.findViewById(R.id.activity_checklist_progress);
        overall_progress_bar.setProgress(overall_progress);
        overall_progress_bar.setMax(checklist.size());

    return convertView;
}

正如您所看到的,我有一个显示进度的TextView,显然值很好。当我开始更改列表中的复选框状态时,一切都正常,唯一的问题是在活动开始时。

progressBar显示了这一点(我认为最大值是'100'而不是'9'):

enter image description here

2 个答案:

答案 0 :(得分:2)

我只是解决了在进度之前设置最大值的问题。即使您在进度后设置了最大值,Android的ProgressBar似乎也不起作用......

改变了这个:

overall_progress_bar.setProgress(overall_progress);
overall_progress_bar.setMax(checklist.size());

对此:

overall_progress_bar.setMax(checklist.size());
overall_progress_bar.setProgress(overall_progress);

答案 1 :(得分:0)

您可以使用Async Task查看进度,在onProgressUpdate中可以设置进度对话框或进度条。