Fragment初步了解
fragment
1.fragment解释:
从英文上来说fragment是碎片和片段的意思,这解释的是相当到位的,因为android中的fragment就像是碎片嵌在了Activity当中的,为构造良好的UI起着至关重要的作用。
fragment和Activity很像,Activity是通过复写onCreate()方法来载入布局文件的,而fragment是通过onCreateView()方法返回一个fragment的布局的。
2.fragment使用前提:
要使用fragment你的SDK版本必须在11以上。在你的Manifest.xml查看版本。
我用的是:
1 <uses-sdk 2 android:minSdkVersion="8" 3 android:targetSdkVersion="18" /> <!-- 要大于11才行 -->
3.下面就通过一个简单的例子来说明fragment的使用方法
MainActivity的class:
1 public class MainActivity extends FragmentActivity implements OnClickListener { //要继承FragmentActivity噢 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 Button btn1 = (Button) findViewById(R.id.btn1); //开第一个fragment 8 Button btn2 = (Button) findViewById(R.id.btn2); //开第二个fragment 9 btn1.setOnClickListener(this); 10 btn2.setOnClickListener(this); 11 } 12 13 @Override 14 public boolean onCreateOptionsMenu(Menu menu) { 15 getMenuInflater().inflate(R.menu.main, menu); 16 return true; 17 } 18 19 @Override 20 public void onClick(View v) { 21 switch (v.getId()) { 22 case R.id.btn1: 23 System.out.println("第一个button"); 24 FragmentTransaction ft = getSupportFragmentManager() //注意这里是getSupportFragmentManager()不是getFragmentManager() 因为是用了android.support.v4.app不是用了android.support.app 25 .beginTransaction(); 26 FirstFragment first = new FirstFragment(); 27 ft.replace(R.id.container, first); 28 ft.commit(); 29 break; 30 case R.id.btn2: 31 FragmentTransaction ft2 = getSupportFragmentManager() 32 .beginTransaction(); 33 SecondFragment second = new SecondFragment(); 34 ft2.replace(R.id.container, second); 35 ft2.commit(); 36 break; 37 default: 38 break; 39 40 } 41 42 } 43 44 }
MainActivity对应的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="wrap_content" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:orientation="horizontal" > 12 13 <Button 14 android:id="@+id/btn1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_weight="1" 18 android:text="切换至第一个fragment" /> 19 20 <Button 21 android:id="@+id/btn2" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_weight="1" 25 android:text="切换至第二个fragment" /> 26 </LinearLayout> 27 <!-- 用来装fragment --> 28 29 30 <RelativeLayout 31 android:id="@+id/container" 32 android:layout_width="match_parent" 33 android:layout_height="match_parent" > 34 </RelativeLayout> 35 36 </LinearLayout>
FirstFragment的class
1 public class FirstFragment extends Fragment { 2 3 public FirstFragment() { 4 } 5 6 @Override 7 public View onCreateView(LayoutInflater inflater, ViewGroup container, 8 Bundle savedInstanceState) { 9 View view = inflater.inflate(R.layout.first_fragment, container, false); 10 // 转向Activity的button 11 Button btn1 = (Button) view.findViewById(R.id.button1); 12 // 开第二个fragment 13 Button btn2 = (Button) view.findViewById(R.id.button2); 14 btn1.setOnClickListener(new btn1()); 15 btn2.setOnClickListener(new btn2()); 16 return view; 17 } 18 19 class btn1 implements OnClickListener { 20 21 @Override 22 public void onClick(View v) { 23 Intent intent = new Intent(); 24 intent.setClass(getActivity(), SecondActivity.class); 25 startActivity(intent); 26 } 27 28 } 29 30 class btn2 implements OnClickListener { 31 32 @Override 33 public void onClick(View v) { 34 FragmentTransaction ft = getFragmentManager().beginTransaction(); //是getFragmentManager不是getSupportFragmentManager 35 SecondFragment second = new SecondFragment(); 36 ft.replace(R.id.container, second); 37 ft.commit(); 38 39 } 40 41 } 42 43 }
FirstFragment所引用的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="@android:color/darker_gray" 6 android:gravity="center" 7 android:orientation="vertical" > 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_gravity="center_vertical|center_horizontal" 13 android:text="我是第一个fragment" /> 14 15 <Button 16 android:id="@+id/button1" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="打开一个activity" /> 20 21 <Button 22 android:id="@+id/button2" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="转向第二个fragment" /> 26 27 </LinearLayout>
SecondFragment的class
1 public class SecondFragment extends Fragment { 2 3 public SecondFragment() { 4 } 5 6 @Override 7 public View onCreateView(LayoutInflater inflater, ViewGroup container, 8 Bundle savedInstanceState) { 9 View view = inflater 10 .inflate(R.layout.second_fragment, container, false); 11 Button btn2 = (Button) view.findViewById(R.id.button111); 12 btn2.setOnClickListener(new Button.OnClickListener() { 13 14 @Override 15 public void onClick(View v) { 16 FragmentTransaction fm = getFragmentManager() 17 .beginTransaction(); 18 FirstFragment first = new FirstFragment(); 19 fm.replace(R.id.container, first); 20 fm.commit(); 21 22 } 23 }); 24 return view; //一定要记住放回一个VIEW对象 25 } 26 }
SecondFragment多引用的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="@android:color/white" 6 android:gravity="center" 7 android:orientation="vertical" > 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_gravity="center_vertical|center_horizontal" 13 android:text="我是第二个个fragment" /> 14 15 <Button 16 android:id="@+id/button111" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="转向第一个fragment" /> 20 21 </LinearLayout>
SecondActivity的class
1 public class SecondActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_second); 7 } 8 9 @Override 10 public boolean onCreateOptionsMenu(Menu menu) { 11 // Inflate the menu; this adds items to the action bar if it is present. 12 getMenuInflater().inflate(R.menu.second, menu); 13 return true; 14 } 15 16 }
SecondActivity对应的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="@android:color/white" 6 android:gravity="center" 7 android:orientation="vertical" > 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_gravity="center_vertical|center_horizontal" 13 android:text="我是第二个个fragment" /> 14 15 <Button 16 android:id="@+id/button111" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="转向第一个fragment" /> 20 21 </LinearLayout>