編輯:Android 開發實例
ListFragment 繼承自 Fragment,因此具備 Fragment 的所有特性:它可以作為 Activity 的一部分存在,讓頁面組裝更為靈活。與一般 Fragment 的差別在於,它的內容以列表形式呈現,預設布局就包含一個 ListView。
由此衍生出一個硬性規則:ListFragment 對應的布局檔中,必須有一個 id 為 @android:id/list 的 ListView,否則會出錯。
一、Activity 程式碼
public class FragmentTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
二、Activity 布局
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:name="com.skw.fragmenttest.MyListFragment"
android:id="@+id/myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
這個 Activity 的布局只放了一個 Fragment,接著看它的實作。
三、MyListFragment
public class MyListFragment extends ListFragment {
private static final String TAG = "##MyListFragment##";
private ListView selfList;
String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan",
"Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
// 設定 ListFragment 預設的 ListView,也就是 @id/android:list
this.setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, cities));
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Log.d(TAG, "onListItemClick");
Toast.makeText(getActivity(), "You have selected " + cities[position],
Toast.LENGTH_SHORT).show();
}
}
這裡使用系統內建的 android.R.layout.simple_list_item_1 作為每一列的樣板。
四、list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- ListFragment 對應的 id 固定為 "@id/android:list" -->
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
如果每一列要顯示的不只一個欄位,可以改用 SimpleAdapter,把資料的 key 與自訂布局中的控制項 id 對應起來。
public class MyListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
final String[] from = new String[] { "title", "info" };
final int[] to = new int[] { R.id.text1, R.id.text2 };
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(
getActivity(), getSimpleData(), R.layout.item, from, to);
this.setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(getActivity(), "You have selected " + position,
Toast.LENGTH_SHORT).show();
}
private List<Map<String, Object>> getSimpleData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "Ferris wheel");
map.put("info", "Suzhou Ferris wheel");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "Flower");
map.put("info", "Roser");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "Disk");
map.put("info", "Song Disk");
list.add(map);
return list;
}
}
item.xml 的內容就是兩個 TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
這個範例建立一個 Activity,同時容納兩個 ListFragment:左邊用系統內建樣板顯示城市清單,右邊則用自訂布局顯示「標題 + 說明」兩行文字。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:name="com.skywang.app.ListFragmentImpl"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:name="com.skywang.app.ListFragmentSelf"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity 本身很單純:
public class ListFragmentTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fragment_test);
}
}
左側的 ListFragmentImpl 使用系統樣板:
public class ListFragmentImpl extends ListFragment {
String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan",
"Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment_impl, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, cities));
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(getActivity(), "You have selected " + cities[position],
Toast.LENGTH_SHORT).show();
}
}
右側的 ListFragmentSelf 則改用 SimpleAdapter 搭配自訂的 two_textview 布局:
public class ListFragmentSelf extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment_self, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
final String[] from = new String[] { "title", "info" };
final int[] to = new int[] { R.id.text1, R.id.text2 };
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(
getActivity(), getSimpleData(), R.layout.two_textview, from, to);
this.setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(getActivity(), "You have selected " + position,
Toast.LENGTH_SHORT).show();
}
private List<Map<String, Object>> getSimpleData() {
// 組出 title / info 的對應資料,略
return null;
}
}
三個布局檔的重點一致:ListFragment 的 ListView id 一律寫成 @id/android:list,差別只在於列的樣板是系統內建或自訂。
執行結果

Android MediaPlayer:音訊播放與控制
MediaPlayer 簡介Android 提供多種方式播放音訊與影片,其中 MediaPlayer 是最常用來控制媒體播放的類別之一。它適合播放應用內建資源、網路串流或
Facebook shimmer 閃光效果庫 Android 使用指南
關於 shimmerShimmer 是 Facebook 開源的一套 Java 函式庫,能讓 Android 上任何 View 元件披上一層流動的高光效果。常見用途是「骨
Android 登入功能實作:從輸入欄位到嘗試次數鎖定
登入畫面是許多 App 的第一道門檻,無論是自家服務的帳號,還是 Facebook、微博這類第三方授權。它看似簡單,實際上要顧到的細節不少:密碼必須遮蔽、錯誤要給明確回饋
Android 顏色選擇器開發:SeekBar 重繪問題排查
Android 沒有內建完整的顏色選擇器,因此許多應用會自行實作。常見做法是上方顯示主色條,下方顯示明暗或飽和度調整,並以 SeekBar 或自訂控制項處理拖動。色彩條實