編輯:關於 Android 程式設計
首先,如果你想在Android 3.0及以下版本使用Fragment,你必須引用android-support-v4.jar這個包。
然後,你寫的Activity不能再繼承自Activity類,而要繼承android.support.v4.app.FragmentActivity,一些其他的父類也有相應的變化。
由於在Android的實現機制中,Fragment和Activity會被分別實例化為兩個不相干的物件,它們之間的聯繫由Activity的一個成員物件FragmentManager來維護。Fragment實例化後會到Activity中的FragmentManager去註冊一下,這個動作封裝在Fragment物件的onAttach中,所以你可以在Fragment中聲明一些回調介面,當Fragment呼叫onAttach時將這些回調介面實例化,如此一來Fragment就能呼叫各個Activity的成員函數了——當然,Activity必須implements這些介面,否則會拋出ClassCastError。
Fragment和Activity的回調機制,又一次完美演繹了OOP的思想!
下面透過一個例子來說明:
首先,我們看下介面


左邊的TextView會根據右邊點擊button的不同而改變。
下面開始介紹程式碼:
1.在layout裡新建fragment1.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:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
可以看出,這裡只有一個TextView。
2.在layout裡新建fragment2.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:background="#ffff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 3" /> </LinearLayout>
這裡是三個button。
3.建立類Fragment1繼承Fragment
package lgx.fram.framents; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } 重寫onCreateView()方法,這裡 return inflater.inflate(R.layout.fragment1, container, false); 這句話是重點。
4.建立類Fragment2繼承Fragment
package lgx.fram.framents; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } } TextView textview; Button button, button2, button3; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button = (Button) getActivity().findViewById(R.id.button); button2 = (Button) getActivity().findViewById(R.id.button2); button3 = (Button) getActivity().findViewById(R.id.button3); textview = (TextView) getActivity().findViewById(R.id.fragment_text); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(button.getText()); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(button2.getText()); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(button3.getText()); } }); } } button = (Button) getActivity().findViewById(R.id.button); 透過這種方法來取得Fragment上面的控件。
5.activity_fragment.xml裡面的程式碼是這個樣子的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="lgx.fram.framents.Fragment1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="lgx.fram.framents.Fragment2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
注意:控件fragment裡的android:name=" "裡面填寫的是你的Fragment類的絕對路徑,id用來標示Fragment。
6.FragmentActivity是最簡單的,只是setContentView,並沒有進行其他改變。看下面
package lgx.fram.framents; import android.app.Activity; import android.os.Bundle; public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); } } 在這裡,我整個小應用就做完了。我這裡的Fragment是透過版面配置檔加入Activity裡的,還有另一種方式是透過程式設計的方式把Fragment加入Activity裡,這裡我簡單敘述。
上面的1、2、3、4都不需要動。
第5步驟,activity_fragment.xml裡面的程式碼變成下面的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > </LinearLayout>
你會發現,我只是去掉了兩個Fragment,把整個LinearLayout加進去了id。
第6個步驟,裡面的註釋已經寫得很清楚了:
package lgx.fram.framents; import android.os.Bundle; import android.app.Activity; import android.view.Display; import android.view.Menu;
@author lenovo 動態加入Fragment主要分為4步:
(1)取得FragmentManager,在Activity中可以直接透過getFragmentManager取得。
(2)開啟一個事務,透過呼叫beginTransaction方法開啟。
(3)向容器內加入Fragment,一般使用replace方法實現,需要傳入容器的id和Fragment的實例。
(4)提交事務,呼叫commit方法提交。
public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); Display display = getWindowManager().getDefaultDisplay(); if (display.getWidth() > display.getHeight()) { Fragment1 fragment1 = new Fragment1(); getFragmentManager().beginTransaction() .replace(R.id.main_layout, fragment1).commit(); } else { Fragment2 fragment2 = new Fragment2(); getFragmentManager().beginTransaction() .replace(R.id.main_layout, fragment2).commit(); } } } 這個程式碼的意思是,橫豎屏顯示不同的Fragment。如果是模擬機測試,請按Ctrl+F11。
Android 入門第二課:Activity 活動的基本用法
前面簡單介紹了 Android 的一些基礎知識作為熱身,接下來要接觸四大元件之一的 Activity(活動)。 Activity 是一種負責維護使用者介面的元件,主要用途
RecyclerView 更新 UI 資料的高級用法:告別 notifyDataSetChanged
用 ListView 的年代,資料一變就呼叫 notifyDataSetChanged() 全量刷新,RecyclerView 當然也吃這一招。但既然 RecyclerV
Android 俄羅斯方塊遊戲完整示例
以下畫面展示 Android 俄羅斯方塊遊戲的執行效果,包含遊戲主介面、方塊堆疊、操作區域與狀態顯示。這類遊戲常用自訂 View 或 Canvas 繪製方塊,並透過計時器
Android 四大基本佈局詳解:LinearLayout、RelativeLayout、FrameLayout、TableLayout
佈局是 UI 開發的地基。本文用可運行的小例子,把四種最常見的佈局容器一次講清楚。 一、LinearLayout:線性佈局 LinearLayout 最關鍵的屬性是 a