Android-Fragment
一、概念
Fragment 表示 Activity 中的行为或用户界面部分。您可以将多个片段组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个片段。您可以将片段视为 Activity 的模块化组成部分,它具有自己的生命周期,能接收自己的输入事件,并且您可以在 Activity 运行时添加或移除片段(有点像您可以在不同 Activity 中重复使用的“子 Activity”)。
Android 在 Android 3.0(API 级别 11)中引入了片段,主要是为了给大屏幕(如平板电脑)上更加动态和灵活的 UI 设计提供支持。由于平板电脑的屏幕比手机屏幕大得多,因此可用于组合和交换 UI 组件的空间更大。利用片段实现此类设计时,您无需管理对视图层次结构的复杂更改。 通过将 Activity 布局分成片段,您可以在运行时修改 Activity 的外观,并在由 Activity 管理的返回栈中保留这些更改。
二、Fragment的生命周期图
三、创建一个Fragment
1、 静态添加Fragment
1)新建两个Fragment的xml文件 样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" android:background ="#feff00" > <TextView android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:text ="This is fragment 1" android:textColor ="#000000" android:textSize ="25sp" /> </LinearLayout >
2)新建一个两个Fragment类,继承自Fragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment { @Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false ); } }
3)在activity_main.xml中加入Fragment的引用,使用android:name前缀来引用具体的Fragment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" android:baselineAligned ="false" > <fragment android:id ="@+id/fragment1" android:name ="com.example.zephon.ncutea_ui3.Fragment1" android:layout_width ="0dip" android:layout_height ="match_parent" android:layout_weight ="1" /> <fragment android:id ="@+id/fragment2" android:name ="com.example.zephon.ncutea_ui3.Fragment2" android:layout_width ="0dip" android:layout_height ="match_parent" android:layout_weight ="1" /> </LinearLayout >
2、 动态添加Fragment
1)修改activity_main.xml
1 2 3 4 5 6 7 8 9 10 <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" android:baselineAligned ="false" android:orientation ="horizontal" > <FrameLayout android:id ="@+id/fragment_container" android:layout_width ="match_parent" android:layout_height ="match_parent" > </FrameLayout > </LinearLayout >
2)修改MainActivity中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 public class MainActivity extends AppCompatActivity { @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment1 f1 = new Fragment1(); fragmentTransaction.add(R.id.fragment_container,f1).commit(); } }
3、实践-制作一个类似QQ的点击切换页面的功能
1)新建两个Fragment的xml页面,也可以用上面的Fragment的xml 2)和上述一样建立两个MyFragment为,继承自Fragment 3)修改activity_main.xml如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" android:baselineAligned ="false" android:orientation ="vertical" > <FrameLayout android:id ="@+id/fragment_container" android:layout_width ="match_parent" android:layout_weight ="10" android:layout_height ="0dp" > </FrameLayout > <LinearLayout android:layout_weight ="1" android:layout_width ="match_parent" android:layout_height ="0dp" > <Button android:id ="@+id/bt_tab1" android:text ="消息" android:layout_weight ="1" android:layout_height ="match_parent" android:layout_width ="0dp" /> <Button android:id ="@+id/bt_tab2" android:text ="联系人" android:layout_width ="0dp" android:layout_weight ="1" android:layout_height ="match_parent" /> </LinearLayout > </LinearLayout >
4)修改MainActivity.java代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 import android.os.Bundle;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View .OnClickListener { private Button bt1; private Button bt2; private Fragment1 f1; private Fragment2 f2; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initEvents(); select(0 ); } private void initViews () { bt1 = findViewById(R.id.bt_tab1); bt2 = findViewById(R.id.bt_tab2); } private void initEvents () { bt1.setOnClickListener(this ); bt2.setOnClickListener(this ); } private void select (int i) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); hidtFragment(ft); switch (i){ case 0 : if (f1 == null ){ f1 = new Fragment1(); ft.add(R.id.fragment_container,f1); }else { ft.show(f1); } break ; case 1 : if (f2 == null ){ f2 = new Fragment2(); ft.add(R.id.fragment_container,f2); }else { ft.show(f2); } break ; } ft.commit(); } private void hidtFragment (FragmentTransaction fragmentTransaction) { if (f1 != null ){ fragmentTransaction.hide(f1); } if (f2 != null ){ fragmentTransaction.hide(f2); } } @Override public void onClick (View v) { switch (v.getId()){ case R.id.bt_tab1: select(0 ); break ; case R.id.bt_tab2: select(1 ); break ; } } }
Github上的好的案例推荐:点我