【Android開發】之Fragment開發1,androidfragment
一直知道Fragment很強大,但是一直都沒有去學習,現在有些閒置時間,所以就去學習了一下Fragment的簡單入門。我也會把自己的學習過程寫下來,如果有什麼不足的地方希望大牛指正,共同進步!
一、Fragment簡介
1.Fragment作為Activity介面的一部分組成出現;
2.可以在一個Activity中同時出現多個Fragment,並且,一個Fragment亦可在多個Activity中使用;
3.在Activity運行過程中,可以添加、移除或者替換Fragment(add()、remove()、replace());
4.Fragment可以響應自己的輸入事件,並且有自己的生命週期,當然,它們的生命週期直接被其所屬的activity的生命週期影響。
那我們為什麼要用Fragment呢?主要目的是用在大螢幕裝置上--例如平板電腦上,支援更加動態和靈活的UI設計。平板電腦的螢幕要比手機的大得多,有更多的空間來放更多的UI組件,並且這些組件之間會產生更多的互動。我們可以把Fragment認為是“小的Activity”,Fragment更加簡潔。
二、Fragment的簡單使用
那我們就簡單的顯示2個Fragment為例來講解一下。
1.在XML中添加Fragment:
建立Fragment1、Fragment2(注意:這裡可能有2個包可以選擇匯入android.app.Fragment或android.support.v4.app.Fragment都是可以的,我這裡選擇使用了前者,但是兩者使用時有區別的,在結尾中我會講到):
Fragment1代碼: 1 package com.example.fragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 import com.example.fragmentdemo.R;11 12 public class Fragment1 extends Fragment {13 @Override14 public View onCreateView(LayoutInflater inflater, ViewGroup container,15 Bundle savedInstanceState) {16 Log.e("TAG", "in");17 return inflater.inflate(R.layout.fragment1, container, false);18 }19 }View Code Fragment2代碼: 1 package com.example.fragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 import com.example.fragmentdemo.R;10 11 public class Fragment2 extends Fragment {12 @Override13 public View onCreateView(LayoutInflater inflater, ViewGroup container,14 Bundle savedInstanceState) {15 return inflater.inflate(R.layout.fragment2, container, false);16 }17 }View Code Fragment1的xml代碼: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#FF69B4" 6 android:orientation="vertical" > 7 8 <TextView 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_gravity="center"12 android:text="這是第一個Fragment" />13 14 </LinearLayout>View Code Fragment2的xml代碼: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#EECBAD" 6 android:orientation="vertical" > 7 8 <TextView 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_gravity="center"12 android:text="這是第二個Fragment" />13 14 </LinearLayout>View Code 我們在activity_main.xml中添加兩個Fragment,代碼如下: 1 <LinearLayout xmlns:android="http://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.fragment.Fragment1" 9 android:layout_width="wrap_content"10 android:layout_height="match_parent"11 android:layout_weight="1" />12 13 <fragment14 android:id="@+id/fragment2"15 android:name="com.example.fragment.Fragment2"16 android:layout_width="wrap_content"17 android:layout_height="match_parent"18 android:layout_weight="1" />19 20 </LinearLayout>View Code MainActivity代碼如下: 1 package com.example.fragmentdemo; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class MainActivity extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) {10 super.onCreate(savedInstanceState);11 setContentView(R.layout.activity_main);12 }13 }View Code
然後運行工程就可以顯示Fragment了,下面是。
2.動態添加Fragment:
我們只需要修改MainActivity和activity_main.xml中的代碼就可以了。
activity_main.xml代碼: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:baselineAligned="false" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:id="@+id/main" 9 android:layout_width="match_parent"10 android:layout_height="match_parent"11 android:orientation="vertical" />12 13 </LinearLayout>View Code MainActivity代碼: 1 package com.example.fragmentdemo; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 import com.example.fragment.Fragment2; 7 8 public class MainActivity extends Activity { 9 10 @Override11 protected void onCreate(Bundle savedInstanceState) {12 super.onCreate(savedInstanceState);13 setContentView(R.layout.activity_main);14 getFragmentManager().beginTransaction()15 .replace(R.id.main, new Fragment2()).commit();16 }17 }View Code
然後運行工程就可以動態顯示Fragment了,下面是。
三、app包下和V4包下的Fragment的區別
1、盡量不要用app包中的fragment,因為這個是在3.0之後才有的,支援的版本太高,在低版本中是是用不了的;
2、android.support.v4.app.Fragment:可以相容到1.6的版本;
3、關於這兩個fragment使用<fragment>標籤的問題:
(1)app.fragment和v4.fragment都是可以使用<fragment>標籤的只是在在使用的時候如果是app.fragment則沒有什麼特殊的地方繼承Activity即可。
(2)當v4.fragment使用<fragment>標籤的時候就要特別注意了:當這個Activity的布局中有<fragment>標籤的時候,這個Activity必須繼承FragmentActivity,否則就會報錯。此時如果不蔔繼成FragmentActivity的話 編譯系統會把<fragment>認為是app包中的Fragment來處理。但是此時我們匯入的是v4包中的FragmentAndroid官方文檔中的Fragment的例子就是以app包中的Fragment來講解的。
(3)app包中關於Fragment的類和方法在V4包中都是有相應的對應的。
4.使用v4.fragment要通過getSupportFragmentManager()方法調用FragmentManager而不是getFragmentManager()方法。
轉載自:http://blog.csdn.net/a465456465/article/details/10415211,感謝。
上面就是Fragment的簡單使用方法,Demo下載,下一節我會講Fragment的詳細使用。歡迎關注,我的CSDN部落格地址:http://blog.csdn.net/u010049692/article/details/38919531。
Android開發,問對於Fragment的一些疑問速來
1、關閉通過remove是最好的方式;
2、此例子說明需要用Fragment來實現tab,可以用ViewPager來管理Fragment,ViewPager會預先載入其中的介面,滑動時流暢
3、
下例說明了怎樣用另一個Fragment替換當前的Fragment,並且在回退堆棧中保留這個Fragment的目前狀態。
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
android 開發 fragment問題
etView方法返回的view是用另一個XML檔案B通過inflate得到的;就是這裡。
這個源碼作者自己封裝了此view,初學者不建議看這種源碼,別搞暈了,簡單點也可以實現的,他的複用性好些。