简体   繁体   English

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

[英]Changing one progressbar changes all progressbars

I have three progress bars that should be updated to show the progress of each subject viewed in a flashcard format. 我有三个进度条,应该更新以显示以抽认卡格式显示的每个主题的进度。 Each progress bar should progress individually. 每个进度条应单独进行。 I am using shared preferences to save the progress in the flashcard activity and then load that data on the activity that handles the progress bars. 我正在使用共享首选项将进度卡中的进度保存下来,然后将数据加载到处理进度条的活动中。 I have only setup my first progressbar to adjust yet all 3 progress bars adjust the same as the first. 我只设置了第一个进度条以进行调整,但所有3个进度条的调整都与第一个相同。

My progressbar xml 我的progressbar 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"
/>

All 3 progressbars look EXACTLY alike except for the id. 除了id以外,所有3个进度条看上去都差不多。

Data saved in flashcard activity 闪存卡活动中保存的数据

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

I define my progressbars in my progressbar activity 我在进度条活动中定义进度条

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

I load my data from my flashcard activity to my progressbar activity 我从抽认卡活动中将数据加载到进度栏活动中

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

I setup my progressbar based on the data loaded from the flashcard activity (i have only setup the intro progress bar so there is no reason I can see for my other progressbars to be changing their progress in sync with this one) 我根据从抽认卡活动中加载的数据来设置进度条(我仅设置了简介进度条,因此没有理由让我的其他进度条更改与此进度同步的进度)

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

Full .xml file showing progressbars 显示进度条的完整.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>

I apologize for skipping a portion of my code. 对于跳过部分代码,我深表歉意。 I am also applying aa drawable to my Progressbars 我也在我的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);

The solution: Add a different drawable for each progressbar 解决方案:为每个进度栏添加一个不同的可绘制对象

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM