編輯:關於 Android 程式設計
前陣子在調整 ViewPager 時,我越發覺得「ViewPager + Fragment」的組合來得靈活好用,於是乾脆把 Fragment 這個知識點重新梳理了一遍,趁機會記錄下來。
Fragment 可以理解為 Activity 中用來承載 UI 的一段行為,或者說是畫面中的一個局部。你可以在同一個 Activity 裡拼裝多個 Fragment,組合成一個多區域的介面;同一個 Fragment 也能在不同 Activity 之間重複使用。
把它想成 Activity 的「模組化區塊」會更好理解:它擁有自己的生命週期,會接收屬於它的輸入事件,並且可以在 Activity 運行期間被動態加入或移除。有了 Fragment,介面就能依使用者操作或裝置特性在執行期組裝,甚至重新組裝。
另外從實作層面看,用 Fragment 而非 Activity 來管理 UI,某種程度上也能繞開 Android 對 Activity 的一些規則限制。
Fragment 的概念是 Android 3.0 才引進的,主要目標就是平板這類大螢幕裝置,希望能支撐更動態、更有彈性的 UI 設計。
平板螢幕比手機大得多,能塞進更多 UI 元件,元件之間的互動也更多。Fragment 正好支援這樣的設計,而且不必你親手去管理複雜的 view hierarchy 變動。把 Activity 的版面拆散到各個 Fragment 之後,你就能在執行期改變 Activity 的外觀,並把這些變化保存到 Activity 管理的 back stack 中。
先寫一個簡單的例子:把兩個 Fragment 放到同一個 Activity 上。
第一個 Fragment 的版面:
<framelayout android:background="#AFEEEE" android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.example.liuhuichao.projecttest.ExampleFragment" xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools"> <textview android:layout_height="match_parent" android:layout_width="match_parent" android:text="這是第一個fragment" /> </framelayout>
接著在 onCreateView 中載入該 Fragment 的版面並完成 view 初始化:
public class ExampleFragment extends Fragment {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_example, container, false);
}
}
第二個 Fragment 寫法幾乎一樣:
<framelayout android:background="#00FA9A" android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.example.liuhuichao.projecttest.ExampleFragmentTwo" xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools"> <textview android:layout_height="match_parent" android:layout_width="match_parent" android:text="這是第二個fragment" /> </framelayout>
public class ExampleFragmentTwo extends Fragment {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_example_fragment_two, container, false);
}
}
Activity 的版面配置:
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="https://schemas.android.com/apk/res/android"> <fragment android:id="@+id/fragmentOne" android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dip" android:name="com.example.liuhuichao.projecttest.ExampleFragment" /> <fragment android:id="@+id/fragmentTwo" android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dip" android:name="com.example.liuhuichao.projecttest.ExampleFragmentTwo" /> </linearlayout>
在 Activity 中關聯這兩個 Fragment:
public class MainFragmentActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainfragment);
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragmentOne, new ExampleFragment());
transaction.replace(R.id.fragmentTwo, new ExampleFragmentTwo());
transaction.commit();
}
}
執行結果如下:
這種做法是我更推薦的方式,最能體現 Fragment 的靈活性。
Fragment 沿用上面兩個,只調整 Activity 的版面:
<linearlayout android:baselinealigned="false" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <button android:id="@+id/btn_show_fragment1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="顯示Fragment1" /> <button android:id="@+id/btn_show_fragment2" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="顯示Fragment2" /> <framelayout android:id="@+id/fragment_container" android:layout_height="match_parent" android:layout_width="match_parent" /> </linearlayout>
Activity 程式碼:
public class MainFragmentActivity extends AppCompatActivity {
private Button btn_show_fragment1;
private Button btn_show_fragment2;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentmaintwo);
btn_show_fragment1 = (Button) findViewById(R.id.btn_show_fragment1);
btn_show_fragment2 = (Button) findViewById(R.id.btn_show_fragment2);
btn_show_fragment1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ExampleFragment ef1 = new ExampleFragment();
// add 把 Fragment 疊到容器上;replace 會先清掉容器內既有 Fragment 再加入新的
ft.add(R.id.fragment_container, ef1);
ft.commit();
}
});
btn_show_fragment2.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.fragment_container, new ExampleFragmentTwo());
ft.commit();
}
});
}
}
用 FragmentManager 搭配 FragmentTransaction 來管理 Fragment 時,感覺很接近資料庫的「交易」概念。查了一下原理,確實有那麼點意思。
android.graphics.Camera:用幾行程式做出簡單的 3D 翻轉效果
Android 的 android.graphics.Camera 並不是拍照用的那個 Camera,而是一個以「相機視點」概念操作 Matrix 的工具類別。透過它
Android Activity 跳轉入門:用 Intent 打開第二個畫面
開發環境:Android 4.1.1 幾乎所有 App 的第一個功能需求都是「換頁」。在 Android 裡,這件事由 Intent 負責,而它同時也是理解整個專案目錄
純手寫一個 Android 朋友圈:列表、多圖選擇與視頻拍攝全流程
朋友圈這種「頭像 + 文字 + 九宮格圖片/視頻」的形態,是社交類應用的標配。前幾天動手寫了一個小 demo,把整個鏈路走了一遍:動態列表渲染、多圖選擇與數量限制、視頻錄
Android 基礎入門教程 8.3.6:Paint API 之 Xfermode 與 PorterDuff 詳解(三)
本節引言 上一節我們認識了 PorterDuffXfermode 構造方法中的關鍵參數 PorterDuff.Mode,並把文件列出的混圖模式逐一驗證了一遍。除了官方文