2016-10-15 77 views
0

我有三個進度條,應更新以顯示以閃卡格式查看的每個主題的進度。每個進度條應該分別進行。我使用共享首選項來保存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個progressbars看起來很相像除了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(); 
} 

我定義我的進度活動我progressbars

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活動加載的數據設置我的進度條兩者均(我只有建立在介紹進度條,所以沒有理由,我可以看到我的其他progressbars正在改變他們的進步同步與此一)

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

完全.xml文件顯示progressbars

<?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> 

我很抱歉跳過我的部分代碼。我也想申請一個可抽拉我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); 
+0

發生了什麼變化?另外發布剩下的'ProgressBar's xmls。 – Onik

+0

所有3個進度條都在同一個.xml文件中,並且它們是從原始文件複製而來的,但它們始終無法粘貼。即使我沒有編寫任何代碼來改變它們,所有三個進度條都在同步progressBarIntro中改變進度。 – th3ramr0d

+0

由於'layout_gravity =「center」'屬性,它們不會相互重疊嗎? – Onik

回答

0

解決辦法:添加不同繪製的每個進度

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