一、Fragment本质是什么?
fragment可以比喻为Activity中的模块,这个模块里有自己的布局,有自己的生命周期,单独处理自己的输入,在activity运行的时候可以加载或者移除fragment模块,它能让程序更加合理和充分地利用大屏幕的空间,因此在平板上应用非常广泛。
二、Fragment的生命周期
①运行状态:当一 个碎片是可见,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态。
②暂停状态:当一个活动进入暂停状态时(由于另一个未占满屏幕的活动被添加到了栈顶 ),与它相关联的可见碎片就会进入暂停状态。
③停止状态:当一个活动进入停止状态时,与它相关联的碎片就会进入到停止状态。
④销毁状态:碎片是依附活动而存在的,因此当活动被销毁时,与它相关联的碎片也会进入销毁状态。
重点注意的几个回调方法:
①onAttach() : 当碎片和活动建立关联时调用。
②onCreateView() : 为碎片创建视图时调用。
③onActivityCreated() : 为碎片创建相关联的活动一定已经创建完毕时的时候调用。
④onDestroyView() : 当与碎片关联的视图被移除的时候调用。
⑤onDetach() : 当碎片和活动解除关联时调用。
三、使用代码形式体验碎片的生命周期
1.修改四中的Right_fragment中的代码
/**
* Created by Administrator on 2017/3/26.
*/
public class Right_fragment extends Fragment{
public static final String TAG ="Right_Fragment";
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG,"onAttach");
}
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG,"onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG,"onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG,"onDetach");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG,"onCreateView");
View view = inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
1.当Right_fragment第一次被加载到屏幕上依次执行以下方法:
2.点击Button按钮弹出新的fragment时,Right_fragment依次执行以下方法(若没有调用addToBackStack()方法,此时的Right_fragment就会进入销毁状态,就会执行onDestroy和onDetach):
3.点击back键,Right_fragment重新回到屏幕,执行以下方法:
4.再一次点击back键,Right_fragment销毁。
四、Fragment用法简介
①静态加载方式
Step 1:定义Fragment的布局,就是fragment显示内容的,这里就不贴出来了。
Step 2:自定义一个Fragment类,需要继承Fragment或者他的子类,重写onCreateView()方法 在该方法中调用:inflater.inflate()方法加载Fragment的布局文件,接着返回加载的view对象
Left_fragment.java
public class Left_fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
Right_fragment.java
public class Right_fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
return view;
}
Step 3: 在需要加载Fragment的Activity对应的布局文件中添加fragment的标签, 记住,name属性是全限定类名,就是要包含Fragment的包名,如:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/left_fragment"
android:layout_weight="1"
android:name="com.example.administrator.fragmenttest.Left_fragment"
/>
<!-- <FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/right_layout"
android:layout_weight="1"
>
</FrameLayout>-->
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/right_fragment"
android:layout_weight="1"
android:name="com.example.administrator.fragmenttest.Right_fragment"
/>
</LinearLayout>
Step 4:
Activity在onCreate( )方法中调用setContentView()加载布局文件即可!
②动态加载方式
Step 1:新建AnotherRightFragment作为另一个右侧碎片,代码同上的LeftFragment.java。
Step 2:修改activity_main.xml为如下内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/left_fragment"
android:layout_weight="1"
android:name="com.example.administrator.fragmenttest.Left_fragment"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/right_layout"
android:layout_weight="1"
>
</FrameLayout>
</LinearLayout>
Step 3:修改MainActivity中的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
replaceFragment(new Right_fragment());//动态添加Right_fragment碎片
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();//获取FragmentManager
FragmentTransaction transaction = fragmentManager.beginTransaction();//开启事务
transaction.replace(R.id.right_layout, fragment);//向容器添加或替换碎片,第一个参数为容器布局ID,第二个为待添加的碎片实例
transaction.addToBackStack(null);
transaction.commit();//提交事务
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
replaceFragment(new Another_Right_fragment());
break;
default:
break;
}
}
小结:从replaceFragment()中的代码可以看出,动态添加碎片主要分以下5步:
1.创建待添加的碎片实例。
2.获取FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法得到。
3.开启一个事务,通过调用beginTransaction()方法完成。
4.向容器内添加或替碎片,一般使用replace()方法完成,第一个参数为容器的id,第二个为待添加的碎片实例。
5.提交事务,调用commit()方法。
运行效果图: