靜態加載:把fragment當控件來使用java
main2.xml在layout中定義fragment控件,android
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 /* 1,Fragment的靜態加載 6 7 在activity的layout文件中聲明Fragment,須要特別注意的是《fragment》中的android:name屬性指定了在laytout中實例化的Fragemnt類 8 9 標示Fragment的方法 10 11 android:id 屬性提供惟一的id 12 13 android:name 屬性提供惟一的name,爲繼承的Fragment類 14 15 Fragment靜態加載比較簡單,能夠當成普通的控件直接寫在activity的佈局中, 16 17 */ 18 <fragment 19 android:id="@+id/frame" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:name="fragmentdemo.example.administrator.fragmentdemo.MyFragment"></fragment> 23 </LinearLayout>
建立一個MyFragment類繼承Fragmentapp
package fragmentdemo.example.administrator.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by Administrator on 2016/5/6. */ public class MyFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /*resource:Fragment須要加載的佈局文件 root:加載layout中父ViewGroup attachToRoot:false,不返回父ViewGroup*/ View view=inflater.inflate(R.layout.fragment,container,false); TextView textView= (TextView) view.findViewById(R.id.text); textView.setText("靜態加載"); return view; } }
fragment加載的文件,fragment.xmlide
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 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:id="@+id/text" 9 10 /> 11 <Button 12 android:text="改變" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/button"/> 16 17 </LinearLayout>
主方法佈局
1 package fragmentdemo.example.administrator.fragmentdemo; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import android.app.Activity; 8 import android.app.Fragment; 9 import android.app.FragmentManager; 10 import android.app.FragmentTransaction; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.widget.RadioGroup; 14 import android.widget.RadioGroup.OnCheckedChangeListener; 15 /*1)Fragment能夠做爲Activity界面的一部分組成出現; 16 (2)能夠在一個Activity中同時出現多個Fragment,而且一個Fragment也能夠在多個Activity中使用; 17 (3)在Activity運行過程當中,能夠添加、移除或替換Fragment; 18 (4)Fragment能夠響應本身的輸入事件,而且有本身的聲明週期,它們的生命週期受宿主Activity的生命週期影響; 19 (5)Fragment在第一次繪製它的用戶界面時,系統會調用onCreateView()方法,此方法返回一個View。(若是不顯示UI,返回null); 20 Fragment兩種加載方式:靜態加載、動態加載。*/ 21 22 public class MainActivity extends Activity implements OnCheckedChangeListener 23 { 24 25 private RadioGroup group; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 // TODO Auto-generated method stub 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 group = (RadioGroup) findViewById(R.id.radiogroup); 33 group.setOnCheckedChangeListener(this); 34 35 } 36 37 @Override 38 public void onCheckedChanged(RadioGroup group, int checkedId) { 39 // TODO Auto-generated method stub 40 41 switch (checkedId) { 42 case R.id.first: { 43 Intent intent=new Intent(this,MainActivity2.class); 44 startActivity(intent); 45 break; 46 47 } 48 case R.id.second: { 49 /*Fragment 動態加載*/ 50 MyFragment2 myFragment2=new MyFragment2();/*建立實例*/ 51 FragmentManager fragmentManager = getFragmentManager();/*獲取到FragmentManager*/ 52 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();/*開啓事務*/ 53 fragmentTransaction.add(R.id.frame,myFragment2); 54 fragmentTransaction.addToBackStack(null);/*經過物理返回鍵返回*/ 55 fragmentTransaction.commit();/*提交事務*/ 56 57 58 break; 59 } 60 case R.id.thrid: { 61 Intent intent=new Intent(this,MainActivity3.class); 62 startActivity(intent); 63 break; 64 65 66 } 67 case R.id.fourth: { 68 Intent intent=new Intent(this,MainActivity4.class); 69 startActivity(intent); 70 break; 71 72 73 } 74 } 75 } 76 77 78 79 80 81 82 }
主方法加載佈局this
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="fragmentdemo.example.administrator.fragmentdemo.MainActivity"> 11 12 <LinearLayout 13 android:id="@+id/frame" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:orientation="vertical" > 17 </LinearLayout> 18 19 20 21 <RadioGroup 22 android:id="@+id/radiogroup" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_alignParentBottom="true" 26 android:gravity="center_horizontal" 27 android:orientation="horizontal" > 28 29 <RadioButton 30 android:id="@+id/first" 31 android:layout_width="0dp" 32 android:layout_height="wrap_content" 33 android:layout_weight="1" 34 android:background="@drawable/radio_pressed" 35 android:button="@null" 36 android:drawableTop="@mipmap/ic_launcher" 37 android:gravity="center_horizontal" 38 android:text="靜態加載" /> 39 40 <RadioButton 41 android:id="@+id/second" 42 android:layout_width="0dp" 43 android:layout_height="wrap_content" 44 android:layout_weight="1" 45 android:background="@drawable/radio_pressed" 46 android:button="@null" 47 android:drawableTop="@mipmap/ic_launcher" 48 android:gravity="center_horizontal" 49 android:text="動態加載" /> 50 51 <RadioButton 52 android:id="@+id/thrid" 53 android:layout_width="0dp" 54 android:layout_height="wrap_content" 55 android:layout_weight="1" 56 android:background="@drawable/radio_pressed" 57 android:button="@null" 58 android:drawableTop="@mipmap/ic_launcher" 59 android:gravity="center_horizontal" 60 android:text="生命週期" /> 61 62 <RadioButton 63 android:id="@+id/fourth" 64 android:layout_width="0dp" 65 android:layout_height="wrap_content" 66 android:layout_weight="1" 67 android:background="@drawable/radio_pressed" 68 android:button="@null" 69 android:drawableTop="@mipmap/ic_launcher" 70 android:gravity="center_horizontal" 71 android:text="傳值通訊" /> 72 </RadioGroup> 73 </RelativeLayout>
在AndroidManifest中註冊spa
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="fragmentdemo.example.administrator.fragmentdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="fragmentdemo.example.administrator.fragmentdemo.MainActivity2" ></activity> <activity android:name=".MainActivity3"></activity> <activity android:name=".MainActivity4"></activity> </application> </manifest>