閱讀778 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android開發小白日記2 (20 Apr) 關於Fragment

很早的時候開發android用的是2.2的SDK,當時界麵就一個activity,可是前些日子重試android開發的時候,發現每次新建一個activity 在新建一個layout的同時,還會有一個非常相似地fragment layout,後來查了下資料,總算明白了在3.0之後,android的sdk就加入了fragment,用戶單屏顯示更多的布局。

下麵的部分內容轉載自:https://blog.csdn.net/guolin_blog/article/details/8881711

為什麼要引入fragment?

我們都知道,Android上的界麵展示都是通過Activity實現的,Activity實在是太常用了,我相信大家都已經非常熟悉了,這裏就不再贅述。

但是Activity也有它的局限性,同樣的界麵在手機上顯示可能很好看,在平板上就未必了,因為平板的屏幕非常大,手機的界麵放在平板上可能會有過分被拉長、控件間距過大等情況。這個時候更好的體驗效果是在Activity中嵌入"小Activity",然後每個"小Activity"又可以擁有自己的布局。因此,我們今天的主角Fragment登場了。

Fragment 實例:

1.平板應用

Fragment通常是嵌套在Activity中使用的,現在想象這種場景:有兩個Fragment,Fragment 1包含了一個ListView,每行顯示一本書的標題。Fragment 2包含了TextView和ImageView,來顯示書的詳細內容和圖片。

如果現在程序運行豎屏模式的平板或手機上,Fragment 1可能嵌入在一個Activity中,而Fragment 2可能嵌入在另一個Activity中,如下圖所示:


而如果現在程序運行在橫屏模式的平板上,兩個Fragment就可以嵌入在同一個Activity中了,如下圖所示:


由此可以看出,使用Fragment可以讓我們更加充分地利用平板的屏幕空間,下麵我們一起來探究下如何使用Fragment。

2. 橫豎屏切換

我能想到的另外一個案例就是iOS自帶的計算器,當是豎屏使用的時候,就是一個隻有簡易計算功能的calculator,但是當通過重力感應切換成橫屏的時候,界麵就顯示成為科學計算器了。這個也許和fragment 有異曲同工之妙吧。

3. QQ、微博、微信界麵下方的標簽欄

這個案例更容易想到了,以前似乎是用activity group實現的,現在用fragment 應該可以輕鬆實現。


通過XML布局文件建立Fragment

新建一個項目叫做Fragments,然後在layout文件夾下新建一個名為fragment1.xml的布局文件:

[html] view plaincopy
  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#00ff00" >  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="This is fragment 1"  
  10.         android:textColor="#000000"  
  11.         android:textSize="25sp" />  
  12.   
  13. </LinearLayout>  

可以看到,這個布局文件非常簡單,隻有一個LinearLayout,裏麵加入了一個TextView。我們如法炮製再新建一個fragment2.xml :

[html] view plaincopy
  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#ffff00" >  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="This is fragment 2"  
  10.         android:textColor="#000000"  
  11.         android:textSize="25sp" />  
  12.   
  13. </LinearLayout>  

然後新建一個類Fragment1,這個類是繼承自Fragment的:
[java] view plaincopy
  1. public class Fragment1 extends Fragment {  
  2.   
  3.     @Override  
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  5.     return inflater.inflate(R.layout.fragment1, container, false);  
  6.     }  
  7.   
  8. }  
我們可以看到,這個類也非常簡單,主要就是加載了我們剛剛寫好的fragment1.xml布局文件並返回。同樣的方法,我們再寫好Fragment2 :
[java] view plaincopy
  1. public class Fragment2 extends Fragment {  
  2.   
  3.     @Override  
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  5.         return inflater.inflate(R.layout.fragment2, container, false);  
  6.     }  
  7.   
  8. }  
然後打開或新建activity_main.xml作為主Activity的布局文件,在裏麵加入兩個Fragment的引用,使用android:name前綴來引用具體的Fragment:
[html] view plaincopy
  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:baselineAligned="false" >  
  5.   
  6.     <fragment  
  7.         android:id="@+id/fragment1"  
  8.         android:name="com.example.fragmentdemo.Fragment1"  
  9.         android:layout_width="0dip"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_weight="1" />  
  12.   
  13.     <fragment  
  14.         android:id="@+id/fragment2"  
  15.         android:name="com.example.fragmentdemo.Fragment2"  
  16.         android:layout_width="0dip"  
  17.         android:layout_height="match_parent"  
  18.         android:layout_weight="1" />  
  19.   
  20. </LinearLayout>  
最後打開或新建MainActivity作為程序的主Activity,裏麵的代碼非常簡單,都是自動生成的:
[java] view plaincopy
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.     }  
  8.   
  9. }  

現在我們來運行一次程序,就會看到,一個Activity很融洽地包含了兩個Fragment,這兩個Fragment平分了整個屏幕,效果圖如下:


動態添加Fragment

你已經學會了如何在XML中使用Fragment,但是這僅僅是Fragment最簡單的功能而已。Fragment真正的強大之處在於可以動態地添加到Activity當中,因此這也是你必須要掌握的東西。當你學會了在程序運行時向Activity添加Fragment,程序的界麵就可以定製的更加多樣化。下麵我們立刻來看看,如何動態添加Fragment。

還是在上一節代碼的基礎上修改,打開activity_main.xml,將其中對Fragment的引用都刪除,隻保留最外層的LinearLayout,並給它添加一個id,因為我們要動態添加Fragment,不用在XML裏添加了,刪除後代碼如下:

[html] view plaincopy
  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/main_layout"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:baselineAligned="false" >  
  6.   
  7. </LinearLayout>  
然後打開MainActivity,修改其中的代碼如下所示:
[java] view plaincopy
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         Display display = getWindowManager().getDefaultDisplay();  
  8.         if (display.getWidth() > display.getHeight()) {  
  9.             Fragment1 fragment1 = new Fragment1();  
  10.             getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();  
  11.         } else {  
  12.             Fragment2 fragment2 = new Fragment2();  
  13.             getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();  
  14.         }  
  15.     }  
  16.   
  17. }  

首先,我們要獲取屏幕的寬度和高度,然後進行判斷,如果屏幕寬度大於高度就添加fragment1,如果高度大於寬度就添加fragment2。動態添加Fragment主要分為4步:

1.獲取到FragmentManager,在Activity中可以直接通過getFragmentManager得到。

2.開啟一個事務,通過調用beginTransaction方法開啟。

3.向容器內加入Fragment,一般使用replace方法實現,需要傳入容器的id和Fragment的實例。

4.提交事務,調用commit方法提交。

現在運行一下程序,效果如下圖所示:


如果你是在使用模擬器運行,按下ctrl + F11切換到豎屏模式。效果如下圖所示:

                                

Fragment之間進行通信

通常情況下,Activity都會包含多個Fragment,這時多個Fragment之間如何進行通信就是個非常重要的問題了。我們通過一個例子來看一下,如何在一個Fragment中去訪問另一個Fragment的視圖。

還是在第一節代碼的基礎上修改,首先打開fragment2.xml,在這個布局裏麵添加一個按鈕:

[html] view plaincopy
  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:background="#ffff00" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="This is fragment 2"  
  11.         android:textColor="#000000"  
  12.         android:textSize="25sp" />  
  13.       
  14.     <Button   
  15.         android:id="@+id/button"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="Get fragment1 text"  
  19.         />  
  20.   
  21. </LinearLayout>  
然後打開fragment1.xml,為TextView添加一個id:
[html] view plaincopy
  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#00ff00" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/fragment1_text"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="This is fragment 1"  
  11.         android:textColor="#000000"  
  12.         android:textSize="25sp" />  
  13.   
  14. </LinearLayout>  
接著打開Fragment2.java,添加onActivityCreated方法,並處理按鈕的點擊事件:
[java] view plaincopy
  1. public class Fragment2 extends Fragment {  
  2.   
  3.     @Override  
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  5.         return inflater.inflate(R.layout.fragment2, container, false);  
  6.     }  
  7.   
  8.     @Override  
  9.     public void onActivityCreated(Bundle savedInstanceState) {  
  10.         super.onActivityCreated(savedInstanceState);  
  11.         Button button = (Button) getActivity().findViewById(R.id.button);  
  12.         button.setOnClickListener(new OnClickListener() {  
  13.             @Override  
  14.             public void onClick(View v) {  
  15.                 TextView textView = (TextView) getActivity().findViewById(R.id.fragment1_text);  
  16.                 Toast.makeText(getActivity(), textView.getText(), Toast.LENGTH_LONG).show();  
  17.             }  
  18.         });  
  19.     }  
  20.   
  21. }  
現在運行一下程序,並點擊一下fragment2上的按鈕,效果如下圖所示:


我們可以看到,在fragment2中成功獲取到了fragment1中的視圖,並彈出Toast。這是怎麼實現的呢?主要都是通過getActivity這個方法實現的。getActivity方法可以讓Fragment獲取到關聯的Activity,然後再調用Activity的findViewById方法,就可以獲取到和這個Activity關聯的其它Fragment的視圖了。



最後更新:2017-04-03 12:56:20

  上一篇:go Android studio使用教程
  下一篇:go Spark Core源碼分析: RDD基礎