【发布时间】:2016-10-15 04:46:19
【问题描述】:
我有三个进度条应该更新,以显示以抽认卡格式查看的每个主题的进度。每个进度条都应该单独进行。我正在使用共享首选项来保存抽认卡活动的进度,然后将该数据加载到处理进度条的活动上。我只设置了我的第一个进度条进行调整,但所有 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"
/>
除了 id 之外,所有 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>
对于跳过我的部分代码,我深表歉意。我还在我的进度条上应用了一个可绘制对象
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);
【问题讨论】:
-
发生了什么变化?还发布其余的
ProgressBars xmls。 -
所有 3 个进度条都在同一个 .xml 文件中,它们是从原始文件中复制而来的,但无论如何都会粘贴。所有三个进度条都在与 progressBarIntro 同步更改进度,即使我没有编写任何代码来更改它们。
-
不是因为
layout_gravity="center"属性而相互重叠吗? -
它们都嵌套在垂直放置的单独线性布局中。当我在设计模式下查看它时,我双击了每一个,每一个都显示了正确的 id
-
如果你注释掉两个没有编写代码的进度条的
findViewById行会发生什么?它们在运行时是否仍显示进度?
标签: android progress-bar