跳轉起來有那麼簡單,我們爲什麼還要使用Fragment呢?這是因爲Fragment相對Activity而言更加的輕量級,使用起來也更加靈活,在一個程序的內部界面切換,儘可能的用Fragment代替Activity會讓我們的APP運行起來更加的流暢,更加的高效,同時也提高了界面的複用性。而卻Fragment在適應多尺寸屏幕方面表現也非常優秀。
首先看一下例子,非常簡單的一個小示例,效果圖如下:
體驗一下就會發現,兩個Fragment跳轉起來要比Activity跳轉的速度快很多。
MainActivity.java代碼如下:
1 /** 2 * MainActivity 主界面 3 * @author codingblock 2015/09/14 4 * 5 */ 6 public class MainActivity extends ActionBarActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 if (savedInstanceState == null) { 13 getSupportFragmentManager() 14 .beginTransaction() 15 .add(R.id.container, new MainFragment()) 16 .commit(); 17 } 18 } 19 }
在MainActivity首先通過getSupportFragmentManager()方法獲取FragmentTransaction的對象,然後用add()方法將MainFragment加載進來,其中引用的佈局文件activity_main.xml如下:
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.codingblock.learnfragment.MainActivity" 7 tools:ignore="MergeRootFrame" />
下面是MainFragment的代碼:
1 /** 2 * MainFragment 主Fragment 3 * @author codingblock 2015/09/14 4 * 5 */ 6 public class MainFragment extends Fragment { 7 8 public MainFragment() { 9 } 10 11 @Override 12 public View onCreateView(LayoutInflater inflater, ViewGroup container, 13 Bundle savedInstanceState) { 14 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 15 rootView.findViewById(R.id.btn_show_other).setOnClickListener(new OnClickListener() { 16 17 @Override 18 public void onClick(View arg0) { 19 getFragmentManager() 20 .beginTransaction() 21 .addToBackStack(null) //將當前fragment加入到返回棧中 22 .replace(R.id.container, new OtherFragment()).commit(); 23 } 24 }); 25 return rootView; 26 } 27 }
在這個Fragment放了一按鈕用於跳轉到另一個Fragment,然後通過FragmentTransaction對象的replace()方法讓OtherFragment把當前Fragment替換掉,在這裏需要注意的是,如果想讓程序可以通過後退方式顯示上一個Fragment的話,需要在替換之前通過addToBackStack()把當前Fragment加入到返回棧中。
它的佈局文件fragment_main.xml代碼如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.codingblock.learnfragment.MainActivity$PlaceholderFragment" > 7 8 <Button 9 android:id="@+id/btn_show_other" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="跳轉到另一個Fragment" /> 13 14 </LinearLayout>
最後一個OtherFragment代碼如下:
1 /** 2 * OtherFragment 另一個Fragment 3 * @author codingblock 2015/09/14 4 * 5 */ 6 public class OtherFragment extends Fragment { 7 8 @Override 9 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 10 View rootView = inflater.inflate(R.layout.fragment_other, container, false); 11 rootView.findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() { 12 13 @Override 14 public void onClick(View arg0) { 15 //從棧中將當前fragment推出 16 getFragmentManager().popBackStack(); 17 } 18 }); 19 return rootView; 20 } 21 }
程序跳轉到這個Fragment之後,如果想返回上一個MainFragment我們可以點擊後退鍵,也可以爲一個按鈕綁定一個單擊事件用FragmentTransaction的popBackStack()方法將當前的Fragment推出棧即可。
它的佈局文件代碼如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tv_show" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="這是另一個Fragment" /> 12 13 <Button 14 android:id="@+id/btn_back" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="返回" /> 18 19 </LinearLayout>