Изменение одной панели прогресса изменяет все панели прогресса

#android #progress-bar

#Android #индикатор выполнения

Вопрос:

У меня есть три индикатора выполнения, которые следует обновить, чтобы показать прогресс каждого предмета, просматриваемого в формате флэш-карты. Каждый индикатор выполнения должен выполняться индивидуально. Я использую общие настройки для сохранения прогресса в действии с флэш-картой, а затем загружаю эти данные в действие, которое обрабатывает индикаторы выполнения. Я настроил только свою первую панель прогресса для настройки, но все 3 индикатора выполнения настраиваются так же, как и первый.

Мой xml progressbar

 <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 панели прогресса выглядят ОДИНАКОВО, за исключением идентификатора.

Данные, сохраненные в активности флэш-карты

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

Я настраиваю свою панель прогресса на основе данных, загруженных из действия флэш-карты (я настроил только индикатор выполнения ввода, поэтому я не вижу причин, по которым мои другие панели прогресса меняют свой прогресс синхронно с этим)

 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>
 

Я прошу прощения за пропуск части моего кода. Я также применяю a к своим индикаторам прогресса

 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. Что меняется? Также опубликуйте остальные ProgressBar XML-файлы.

2. Все 3 панели прогресса находятся в одном и том же XML-файле, и они были скопированы из исходного, но я всегда вставляю их. Все три панели прогресса изменяют прогресс синхронно с progressBarIntro, хотя я не написал никакого кода для их изменения.

3. Разве они не перекрывают друг друга из-за layout_gravity="center" атрибута?

4. все они вложены в отдельные линейные описания, расположенные вертикально. когда я просматриваю его в режиме разработки, я дважды щелкнул по каждому из них, и каждый из них показал правильный идентификатор

5. Что произойдет, если вы закомментируете findViewById строки для двух индикаторов выполнения, для которых вы не написали код? Они все еще показывают прогресс во время выполнения?

Ответ №1:

Решение: добавьте разные чертежи для каждой панели прогресса

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