(1) 建立fragment:建立自定義Fragment類繼承自Fragment類,同時將自定義Fragment類與Fragment視圖綁定(將layout轉換成View)android
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
inflater用於綁定Fragment的佈局文件,同時將該佈局轉換成View對象並返回;container爲Fragment的UI所在的父容器。返回值爲Fragment顯示的UI,若不顯示,則返回null。git
inflate(int resource, ViewGroup root, boolean attachToRoot)
resource爲Fragment須要加載的佈局文件;root爲加載Fragment的父ViewGroup,也就是onCreateView傳遞進來的container;attachToRoot爲是否返回父ViewGroup。github
(2) 使用fragment:在父視圖中引入fragment,靜態加載必須指定name屬性以及一個惟一標識符,標識符能夠爲id或者tagide
<!--指定在layout中實例化的Fragment類,須要爲「包名.類名」的完整形式--> android:name <!--惟一標識,id和tag可任選其一,不可二者都沒有--> android:id android:tag
(3) 監聽事件:若在父視圖對應的類中設置監聽事件,能夠直接訪問fragment中的子組件;若在Fragment的類中設置,則必須經過inflate()返回的View對象訪問Fragment中的子組件(view.findViewById(id))。函數
MyFragment視圖:佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MyFragment類:this
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //將layout佈局轉換成View對象 View view = inflater.inflate(R.layout.myfragment, container, false); //必須經過view對象對其子組件進行訪問 TextView textView = (TextView) view.findViewById(R.id.fragment_text); textView.setText("這裏是fragment"); //返回Fragment顯示UI return view; } }
引用fragment的父視圖:code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.studying.StaticFragmentActivity"> <fragment android:tag="fragment" android:name="com.joahyau.studying.MyFragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
父視圖對應的類設置事件監聽:xml
public class StaticFragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static_fragment); //可直接經過findViewById訪問 findViewById(R.id.fragment_text).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(StaticFragmentActivity.this, "點擊了文本", Toast.LENGTH_SHORT).show(); } }); } }
(1) 獲取事務管理器:對Fragment進行的添加、移除、替換等操做,均爲事務。需經過如下代碼獲取事務管理器,從而對fragment進行動態操做。對象
FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction();
(2) 建立Fragment對象:建立須要加載的fragment,然後經過add或replace等方法實現動態加載。
佈局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="io.github.joahyau.studying.DynamicFragmentActivity"> <Button android:id="@+id/load" android:text="加載" android:layout_width="match_parent" android:layout_height="80dp" /> <LinearLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> </LinearLayout>
Java:
public class DynamicFragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dynamic_fragment); findViewById(R.id.load).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //獲取事務管理器 FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //建立fragment,並將其動態加載到id位container的佈局中 MyFragment myFragment = new MyFragment(); fragmentTransaction.add(R.id.container, myFragment); //提交事務 fragmentTransaction.commit(); } }); } }
(1)在Activity中獲取Fragment對象;
(2)建立Bundle對象並傳入數據;
(3)將Bundle對象傳遞給Fragment對象;
(4)在Fragment中獲取Bundle對象並拆包獲得數據。
範例:Activity中只有一個id爲send的Button,MyFragment中只有一個TextView,這裏就再也不放佈局代碼了。
Activity:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.send).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //建立Fragment對象 MyFragment myFragment = new MyFragment(); //建立Bundle對象並傳入數據 Bundle bundle = new Bundle(); bundle.putString("info", "這裏是向Fragment傳遞的數據"); myFragment.setArguments(bundle); //加載Fragment FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.add(R.id.layout, myFragment, "myfragment"); beginTransaction.commit(); } }); } }
Fragment:
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); TextView tv = (TextView) view.findViewById(R.id.text); //獲取數據 String text = getArguments().get("info") + ""; tv.setText(text); return view; } }
(1)在Fragment中建立做爲容器的數據對象,並建立getter和setter;
(2)在Activity中獲取FragmentManager;
(3)經過事務管理器的findFragmentById或findFragmentByTag方法,得到fragment對象;
(4)經過得到的fragment對象調用容器的setter方法進行傳值。
範例:這裏的佈局與動態加載的佈局惟一不一樣的就是將send按鈕放在了Fragment裏面,其它相同。
Fragment:
public class MyFragment extends Fragment { private Button btn; private String received;//做爲容器的對象 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); TextView tv = (TextView) view.findViewById(R.id.text); tv.setText("這裏是Fragment"); btn = (Button) view.findViewById(R.id.send); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "成功接收\"" + getReceived() + "\"", Toast.LENGTH_SHORT).show(); } }); return view; } public String getReceived() { return received; } public void setReceived(String received) { this.received = received; } }
Activity:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getFragmentManager(); MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment); myFragment.setReceived("this is a test."); } }
(1)在Fragment中寫一個回調接口;
(2)在activity中實現這個回調接口,實現的函數用於傳值;
(3)重寫Fragment中onAttach,在其中建立一個接口對象,獲得傳遞過來的activity(個人理解是這個接口其實至關於傳遞過來的activity的一個父類,這一步是用到了多態的特性);
(4)用獲得的接口對象進行傳值。
Fragment:
public class MyFragment extends Fragment { private SendData sendData; @Override public void onAttach(Activity activity) { super.onAttach(activity); //獲取實現的接口對象 sendData = (SendData) activity; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); TextView tv = (TextView) view.findViewById(R.id.text); tv.setText("這裏是Fragment"); //經過接口對象傳遞數據 sendData.sendMsg("this is a test."); return view; } //定義一個回調接口 public interface SendData{ void sendMsg(String str); } }
Activity:
public class MainActivity extends Activity implements MyFragment.SendData{ private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.send); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyFragment myFragment = new MyFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.add(R.id.layout, myFragment); beginTransaction.commit(); } }); } //實現SendData接口,接收數據 @Override public void sendMsg(String str) { Toast.makeText(this, "成功接收\"" + str + "\"", Toast.LENGTH_SHORT).show(); } }