プログレス バーの例をたくさん見てきましたが、それらはすべて、新しいスレッドまたは非同期タスクの作成を伴います。これらの種類の進行状況バーは、タスクが完了するとポップアップ、更新、および非表示になります。
ただし、進行状況をハードコーディングすることで実際に更新できる永続的な ProgressBar をアプリに作成することに興味があります。
ここにコード例があります:
ProgressBar levelProgress;
levelProgress = (ProgressBar)findViewById( R.id.progressBarLevel);
levelProgress.setMax(100);
levelProgress.setProgress(50); //this is what I wanted to do
ProgressBar をそのままにしておきたいので、上記のコードでは、レイアウトに永続的な ProgressBar が半分完成することを期待していました。私が実際に持っているのは、空の ProgressBar です。
最後の質問は、プログレス バーの更新をハード コーディングする方法はありますか? それとも AsyncTask または実行可能なスレッドを介してのみ利用できますか?
編集:これをフラグメントとして実装していることを忘れていました。もう少し完全なコードは次のようになります
public class myLevelDisplayActivity extends Fragment{
LinearLayout ll;
static FragmentActivity fa;
ProgressBar levelProgress;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.levelDisplay, container, false);
levelProgress = (ProgressBar) ll.findViewById( R.id.progressBarLevel);
levelProgress.setMax(100);
levelProgress.setProgress(0);
levelProgress.setProgress(10);
levelProgress.setProgress(50);
return ll;
}
levelDisplay.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:ignore="HardcodedText,ExtraText,ExtraText,ExtraText,ExtraText,ExtraText" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" >
<ImageView
android:id="@+id/imageViewCurrentLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0.26"
android:orientation="vertical">
<TextView
android:id="@+id/textViewLevelTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Level Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ProgressBar
android:id="@+id/progressBarLevel"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3.16"
android:max="100"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="3.16" >
<TextView
android:id="@+id/textViewCurrent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewSeparator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewNextLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Level"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>