更改一个进度条会更改所有进度条

时间:2016-10-15 04:46:20

标签: android progress-bar

我有三个进度条应该更新,以显示以flashcard格式查看的每个主题的进度。每个进度条应单独进行。我使用共享首选项来保存闪卡活动中的进度,然后将该数据加载到处理进度条的活动上。我只设置了我的第一个进度条进行调整,但所有3个进度条的调整与第一个相同。

我的进度条xml

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/introProgress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="4"
    android:progress="0"
    android:indeterminate="false"
    android:layout_gravity="center"
    android:padding="5dp"
/>

<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/howToStudyProgress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    android:indeterminate="false"
    android:layout_gravity="center"
    android:padding="5dp"
/>

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/proceduresProgress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    android:indeterminate="false"
    android:layout_gravity="center"
    android:padding="5dp"
/>

所有3个进度条看起来都非常相似,除了id。

闪存卡活动中保存的数据

private void savePosition(){
    SharedPreferences sharedPrefs = this.getActivity().getSharedPreferences("Intro", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.putInt("Intro Progress", progress);
    editor.putInt("Intro Position", position);
    editor.putBoolean("Viewed State", viewed);
    editor.commit();
}

我在进度条活动中定义了进度条

progressBarIntro = (ProgressBar)myView.findViewById(R.id.introProgress);
progressBarStudy = (ProgressBar)myView.findViewById(R.id.howToStudyProgress);
progressBarProcedures = (ProgressBar)myView.findViewById(R.id.proceduresProgress);

我将我的闪卡活动中的数据加载到我的进度条活动

private void loadPreferences()
{
    SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("Intro", Context.MODE_PRIVATE);
    introProgressValue = sharedPreferences.getInt("Intro Progress", 0);
    introViewed = sharedPreferences.getBoolean("Viewed State", false);
}

我根据flashcard活动加载的数据设置了我的进度条(我只设置了介绍进度条,所以没有理由我可以看到我的其他进度条改变它们与此同步的进度)

private void setupProgressBars(){
if(!introViewed){
    progressBarIntro.setProgress(0);
}
if(introViewed){
    progressBarIntro.setProgress(introProgressValue + 1);
}
progressBarIntro.setMax(4);
}

显示进度栏的完整.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_alignParentTop="true"
    >

    <!--
    this goes in the above scrollview
    android:layout_above="@+id/adView"
     -->


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- INTRODUCTION -->
        <LinearLayout
            android:id="@+id/introView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/button_no_border_selector"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:onClick="introduction"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="INTRODUCTION"
                android:gravity="center"
                android:padding="5dp"
                />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/introProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="0"
                android:layout_gravity="center"
                android:padding="5dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0/10"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Progress"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="--"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Reviewed"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layoutview_background"
            />

        <!-- HOW TO STUDY -->
        <LinearLayout
            android:id="@+id/howToStudyView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:drawable/list_selector_background"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:onClick="howToStudy"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="HOW TO STUDY"
                android:gravity="center"
                android:padding="5dp"
                />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/howToStudyProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="0"
                android:layout_gravity="center"
                android:padding="5dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0/10"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Progress"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="--"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Reviewed"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layoutview_background"
            />

        <!-- BOARD PROCEDURES -->
        <LinearLayout
            android:id="@+id/boardProceduresView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:drawable/list_selector_background"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:onClick="boardProcedures"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="BOARD PROCEDURES"
                android:gravity="center"
                android:padding="5dp"
                />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/proceduresProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="0"
                android:layout_gravity="center"
                android:padding="5dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0/10"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Progress"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="--"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Reviewed"
                        android:gravity="center"
                        android:padding="5dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layoutview_background"
            />

    </LinearLayout>

</ScrollView>
<!-- ADVIEW GOES HERE-->

</RelativeLayout>

我为跳过部分代码而道歉。我也在将一个drawable应用到我的Progressbars

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.progress);

progressBarIntro = (ProgressBar)myView.findViewById(R.id.introProgress);
progressBarIntro.setProgressDrawable(drawable);

temp = (ProgressBar)myView.findViewById(R.id.temp);
temp.setProgressDrawable(drawable);

1 个答案:

答案 0 :(得分:0)

解决方案:为每个进度条添加不同的drawable

Resources res = getResources();
Drawable drawableIntro = res.getDrawable(R.drawable.progress);
Drawable drawableTemp = res.getDrawable(R.drawable.progress);

progressBarIntro = (ProgressBar)myView.findViewById(R.id.introProgress);
progressBarIntro.setProgressDrawable(drawableIntro);

temp = (ProgressBar)myView.findViewById(R.id.temp);
temp.setProgressDrawable(drawableTemp);