Android开发学习—— Fragment

#Fragment
* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容;必须被嵌入activity中使用。
* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity
* 定义布局文件作为Fragment的显示内容

Fragment调用getActivity()获取它所在的activityactivity调用FragmentManager的findFragmentById()来获取fragment

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void click01(View v){
        fragment fg1 = new fragment();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.f1,fg1);
        ft.commit();

    }
    public void click02(View v){
        fragment1 fg2 = new fragment1();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.f1,fg2);
        ft.commit();
    }
    public void click03(View v){
        fragment2 fg3 = new fragment2();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.f1,fg3);
        ft.commit();
    }

}
public class fragment1 extends Fragment {

    @Nullable
    @Override
    //返回的view对象会作为fragment的内容显示在屏幕上
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v =  inflater.inflate(R.layout.fragment1,null);
        return  v;
    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.xnmeng.hello.MainActivity">

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment01"
        android:onClick="click01"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment02"
        android:onClick="click02"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment03"
        android:onClick="click03"/>
   </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/f1">

    </FrameLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="红色"/>

</LinearLayout>

结果:

点击左边的三个按钮,右边显示相应的颜色。


###生命周期
* fragment切换时旧fragment对象会销毁,新的fragment对象会被创建
###低版本兼容
* 在support-v4.jar包中有相关api,也就是说fragment可以在低版本模拟器运行

原文地址:https://www.cnblogs.com/mengxiao/p/6226720.html