Android中Fragment的添加方法总结
前言
Fragment和Activity的关系
1.把Fragment认为模块化的一段Activity
2.它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除
3.Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当Activity暂停时,它拥有的所有的Fragment们都暂停了,当Activity销毁时,它拥有的所有Fragment们都被销毁,上图是Activity的生命活动周期中和Fragment的生命活动周期的一个比较。其中也标识出了Activity的状态和Fragment的对应关系。
Fragment的作用
主要是为了支持更动态、更灵活的界面设计
Fragment的添加方法
在Activity中添加Fragment的方法主要有两种:
静态添加
在Activity的xml文件中直接添加Fragment。
动态添加
在Activity的.java文件中动态添加。
静态添加方法
实现流程
注意
在添加库的时候,这里可以采用android.support.v4.app.Fragment或android.app.Fragment均可。
动态添加方法
实现流程
注意
在添加库的时候,这里只能采用android.app.Fragment。
代码
Activity
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.annotation.Nullable;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
/**第二个fragment的操作**/
//1、获得FragmentManager
FragmentManager fragmentManager=getFragmentManager();
//2、获得FragmentTransaction
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//3、构造一个Fragment
FragmentTwo fragmentTwo=new FragmentTwo();
//4、动态添加一个Fragment
fragmentTransaction.add(R.id.fragment_2,fragmentTwo);
fragmentTransaction.commit();
}
/**第一个Fragment**/
/**
* 这种方法可以采用android.support.v4.app.Fragment,但是
*/
public static class FragmentOne extends android.support.v4.app.Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//FragmentOne将fragment_one.xml作为Fragment的布局文件
return inflater.inflate(R.layout.fragmet_one,container,false);
}
}
/**第二个Fragment**/
/**
* 只能采用android.app.Fragment
*/
public static class FragmentTwo extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmet_one,container,false);
}
}
}
XML文件
Activity对应的xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.kimhlo.smartfeedapplication.MainActivity">
<!--第一个fragment-->
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/fragment_1"
android:name="com.android.kimhlo.smartfeedapplication.InformationActivity$FragmentOne">
</fragment>
<!--第二个fragment-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/fragment_2">
</FrameLayout>
</LinearLayout>
Fragment显示内容的xml文件,fragment_one.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment one"/>
</LinearLayout>