編輯:關於 Android 程式設計
在 Android 的眾多控制項中,ProgressBar(進度條)與 ListView(列表視圖)是兩個既實用又稍微複雜的元件。本文將以一個結合兩者的完整範例,帶領初學者深入了解它們的用法與實作細節。
ProgressBar 經常用於檔案載入、資料處理、下載等需要等待進度的場合,有圓形與長條形兩種樣式。
ListView 則用於以列表的形式展示內容,是呈現大量資料時的首選元件。
要理解 ListView,首先要掌握它的三個關鍵組成:
根據適配器的類型不同,列表主要分為三種:
以下範例將 ProgressBar 與 ListView 放在同一個 Activity 中。點擊按鈕可驅動進度條變化,而 ListView 則展示了自訂的列表項目與點擊事件。
package com.example.activity_02;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ProgressBar myProgressBar1 = null;
private ProgressBar myProgressBar2 = null;
private Button myButton = null;
private ListView myListView = null;
private SimpleAdapter myAdapter = null;
private int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myProgressBar1 = (ProgressBar) findViewById(R.id.myProgressBar1);
myProgressBar2 = (ProgressBar) findViewById(R.id.myProgressBar2);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
myListView = (ListView) findViewById(R.id.myListView);
// 生成動態陣列,並且轉載資料
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "I'm title!");
map.put("ItemText", "I'm content!I'm content!I'm content!");
mylist.add(map);
}
// 生成適配器,陣列 ===> ListItem
myAdapter = new SimpleAdapter(this,
mylist, // 資料來源
R.layout.my_listitem, // ListItem 的 XML 實現
new String[] { "ItemTitle", "ItemText" }, // 動態陣列與 ListItem 對應的子項
new int[] { R.id.ItemTitle, R.id.ItemText }); // ListItem XML 中的兩個 TextView ID
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(new OnItemClickListenerImpl());
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == 1) finish();
if (item.getItemId() == 2) new AlertDialog.Builder(this)
.setTitle("About").setMessage("Powerd By Empty.").show();
return super.onMenuItemSelected(featureId, item);
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
if (i == 0) {
myProgressBar1.setVisibility(View.VISIBLE);
myProgressBar2.setVisibility(View.VISIBLE);
} else if (i < myProgressBar1.getMax()) {
myProgressBar1.setProgress(i);
myProgressBar1.setSecondaryProgress(i + 10);
// 預設模式的進度條無法設置狀態
// myProgressBar2.setProgress(i);
} else {
i = 0;
}
i += 10;
System.out.println("ClickMe");
}
}
private class OnItemClickListenerImpl implements OnItemClickListener {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// 獲得選中項的 HashMap 對象
HashMap<String, String> map = (HashMap<String, String>) myListView.getItemAtPosition(arg2);
String title = map.get("ItemTitle");
String content = map.get("ItemText");
Toast.makeText(getApplicationContext(),
"你選擇了第" + arg2 + "個Item,itemTitle的值是:" + title + "itemContent的值是:" + content,
Toast.LENGTH_SHORT).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/myListItem"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="3dip"
android:paddingLeft="10dip" >
<TextView
android:id="@+id/ItemTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip" >
</TextView>
<TextView
android:id="@+id/ItemText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tip1"
android:textSize="20dp" />
<ProgressBar
android:id="@+id/myProgressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone" />
<ProgressBar
android:id="@+id/myProgressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tip2" />
<TextView
android:id="@+id/myTextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tip3"
android:textSize="20dp" />
<ListView
android:id="@+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
透過這個範例,我們可以掌握幾個重點:
progressBarStyleHorizontal(長條形),可以透過 setProgress() 設定精確進度;而 myProgressBar2 使用預設的 progressBarStyle(圓形轉圈),它屬於不確定模式的進度條,無法設定具體進度數值。ArrayList<HashMap> 資料來源、自訂的列表項佈局檔,以及「資料鍵名」與「控制項 ID」的對應陣列結合起來,就能輕鬆將資料渲染到列表上。OnItemClickListener,可以在使用者點擊某個列表項時,用 getItemAtPosition() 取得該項的 HashMap 資料,进而讀取出對應的欄位值並做出回應。掌握 ProgressBar 與 ListView 這兩個元件,是踏入 Android UI 開發的重要一步。特別是理解 SimpleAdapter 如何作為資料與視圖之間的橋樑,將為日後開發更複雜的列表介面奠定良好的基礎。
Android XML 資料解析實戰:SAX、DOM 與 PULL 的選擇之道
XML 基礎入門 今天我們來認識另一種非常重要的資料交換格式 —— XML。XML 是 Extensible Markup Language 的縮寫,意為「可擴充的標記語
詳解 Android MVP 開發模式
本文主要講解 MVP 開發模式,並通過一個用戶登錄的具體實例演示其落地方式。 一、簡介 MVP(Model View Presenter)是經典 MVC(Model Vi
關於 ListView 的全面總結:適配器、原理與內存優化
網上關於 ListView 的介紹很多,我挑選了一些內容整合於此,僅供參考。 在 Android 開發中,ListView 是比較常用的組件。 官方 API 地址:htt
Android 文字轉語音(TTS)實戰
視覺反饋固然是最直觀的信息傳遞方式,但它要求用戶必須盯著屏幕。當用戶在做飯、開車或者乾脆把手機揣在兜裡時,屏幕就失去了作用。Android 提供了強大的 Text-to-