編輯:中階開發
Fragment 是 Android 3.0 平板系統的重要特性,今天 Android123 帶來兩個實戰範例,一起來看 Fragment 的實際用法。
public class FragmentContextMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContextMenuFragment content = new ContextMenuFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, content).commit(); //在 Activity 中透過這個方式與 Fragment 溝通
}
下面 ContextMenuFragment 是我們從 Fragment 派生的子類,其中重寫了 onCreateView 來布局自己的 Fragment
public static class ContextMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_context_menu, container, false);
registerForContextMenu(root.findViewById(R.id.long_press));
return root;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "菜單1");
menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "菜單2");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.a_item:
Log.i("CWJ", "Item 1a was chosen");
return true;
case R.id.b_item:
Log.i("CWJ", "Item 1b was chosen");
return true;
}
return super.onContextItemSelected(item);
}
}
}
涉及到的布局檔 fragment_context_menu.XML 源碼
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/fragment_context_menu_msg" />
<Button android:id="@+id/long_press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/long_press">
<requestFocus />
</Button>
</LinearLayout>
public class FragmentHideShow extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_hide_show);
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
//以上兩行程式碼是在 Activity 中為 Fragment 裡的按鈕加入監聽事件
}
void addShowHideListener(int buttonId, final Fragment fragment) {
final Button button = (Button)findViewById(buttonId);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out); //為 Fragment 設定淡入淡出效果,Android 開發網提示這裡這兩個動畫資源是 Android 內部資源,無需我們手動定義。
if (fragment.isHidden()) {
ft.show(fragment);
button.setText("隱藏");
} else {
ft.hide(fragment);
button.setText("顯示");
}
ft.commit();
}
});
}
public static class FirstFragment extends Fragment {
TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findViewById(R.id.msg);
((TextView)tv).setText("The fragment saves and restores this text.");
mTextView = (TextView)v.findViewById(R.id.saved);
if (savedInstanceState != null) {
mTextView.setText(savedInstanceState.getCharSequence("text"));
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence("text", mTextView.getText());
}
}
public static class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findViewById(R.id.msg);
((TextView)tv).setText("The TextView saves and restores this text.");
((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);
return v;
}
}
}
涉及資源布局檔 fragment_hide_show.XML 程式碼:
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Demonstration of hiding and showing fragments." />
<LinearLayout android:orientation="horizontal" android:padding="4dip"
android:gravity="center_vertical" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/frag1hide"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hide" />
<fragment android:name="com.android123.FragmentHideShow$FirstFragment"
android:id="@+id/fragment1" android:layout_weight="1"
android:layout_width="0px" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:padding="4dip"
android:gravity="center_vertical" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/frag2hide"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hide" />
<fragment android:name="com.android123.FragmentHideShow$SecondFragment"
android:id="@+id/fragment2" android:layout_weight="1"
android:layout_width="0px" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
有關布局檔 labeled_text_edit.XML 的程式碼為
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/msg"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip" />
<EditText android:id="@+id/saved"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/green"
android:text="@string/initial_text"
android:freezesText="true">
<requestFocus />
</EditText>
</LinearLayout>
動手打造 iPhone 與 Android 的瀏覽器型應用程式
前言:本文是「Android 與 iPhone 瀏覽器之戰」系列的第二篇。上一篇談的是兩者瀏覽器共同的核心引擎 WebKit;這次要往前多走一步,實際做出一個能跑在 iP
Fragment 對比 Activity - Android 碎片介紹
Fragment 是 Android Honeycomb 3.0 新增的概念,名字雖叫「碎片」,卻和 Activity 十分相似。下面 Android123 來介紹 An
Android 3.0 Honeycomb 底層細節:硬體加速、Renderscript 與平板 UI
Honeycomb:Android 平板時代的起點 Android 3.0,代號 Honeycomb,是 Android 第一個主要針對平板設計的版本,也是首個導入硬體加
HTML5 在 Android 上的視覺化 UI:Canvas、CSS3 與表單新特性
HTML5 與行動瀏覽器 HTML5 不只是新的標記語言,也整合了 JavaScript 與 CSS 的新能力。對於 iPhone 與 Android 等行動裝置,Can