Fragment介绍
来源:互联网 发布:mysql-python和mysqldb 编辑:程序博客网 时间:2024/06/05 06:40
Fragment是Activity的子模块,用于嵌入到Activity中来使用。
Fragment简介
Fragment是Android 3.0 引入的新的API,原先,Fragment是为了适应大屏幕的平板而设计的,这是因为Android平板拥有了较大的屏幕面积,就是说“大”,够用:),可以容纳更多的UI,而Fragment也可以称为是Activity的片段,将Activity分割成多个部分后包含多种UI来满足Android平板的大屏幕。
虽然说Fragment是Activity的“片段”,但是Fragment也拥有自己的生命周期,但是“身为下级还是要受老板控制的”,Fragment也得乖乖的听老板的话,老板往东他就不敢往西:(,也就是说Fragment的生命周期受到了他所在的Activity的生命周期的控制。
其中啊~~
- Fragment作为Activity部门小弟,作为小弟,可以知道向部门大佬打报告(用getActivity()来获取Fragment他所在的Activity),同时,部门老大,挥挥手就可以“增删改(换)查”:)…(老板就是威武,可以调用FragmentManager的add(),remove(),replace(),findFragmentById())
- 对于部门大佬(Activity),可以作用千千万小弟(不卡死就行…);作为部门小弟,纵使心理千万无奈,也摆脱不了上头千千万大佬的命运:)。
- 一个Activity可以拥有多个Fragment,一个Fragment也可以被多个Activity复用,或者是被同一个Activity复用。
代码
一个Activity包含多个Fragment
其中一个Fragment的代码:
package com.notzuonotdied.test_fragment;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Notzuonotdied on 2016/8/21. */public class TestFragment01 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.testfragment01, container, false); }}
对应的布局文件
<?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="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="TestFragment01" android:textColor="#000000" android:textSize="30sp" /></LinearLayout>
MainActivity中的代码如下显示:
解析我就不写了,看代码吧,感觉没什么好说的:)。
package com.notzuonotdied.test_fragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.FrameLayout;public class MainActivity extends FragmentActivity implements View.OnClickListener{ private TestFragment01 FG1; private TestFragment02 FG2; private TestFragment03 FG3; private TestFragment04 FG4; private android.widget.FrameLayout idcontent; private android.widget.Button tv01; private android.widget.Button tv02; private android.widget.Button tv03; private android.widget.Button tv04; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); setSelect(0); } /** * 设置响应事件 * */ private void initEvent() { tv04.setOnClickListener(this); tv03.setOnClickListener(this); tv02.setOnClickListener(this); tv01.setOnClickListener(this); } /** * 设置选择的Fragment * */ private void setSelect(int id) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); hideFragment(transaction); showFragment(transaction, id); transaction.commit(); } /** * 显示Fragment * */ private void showFragment(FragmentTransaction transaction, int id) { switch (id) { case 0: if (FG1 != null) { transaction.show(FG1); } else { FG1 = new TestFragment01(); transaction.add(R.id.id_content, FG1); } break; case 1: if (FG2 != null) { transaction.show(FG2); } else { FG2 = new TestFragment02(); transaction.add(R.id.id_content, FG2); } break; case 2: if (FG3 != null) { transaction.show(FG3); } else { FG3 = new TestFragment03(); transaction.add(R.id.id_content, FG3); } break; case 3: if (FG4 != null) { transaction.show(FG4); } else { FG4 = new TestFragment04(); transaction.add(R.id.id_content, FG4); } break; } } /** * 隐藏 * */ private void hideFragment(FragmentTransaction transaction) { if (FG1 != null) { transaction.hide(FG1); } if (FG2 != null) { transaction.hide(FG2); } if (FG3 != null) { transaction.hide(FG3); } if (FG4 != null) { transaction.hide(FG4); } } /** * 初始化View * */ private void initView() { this.tv04 = (Button) findViewById(R.id.tv04); this.tv03 = (Button) findViewById(R.id.tv03); this.tv02 = (Button) findViewById(R.id.tv02); this.tv01 = (Button) findViewById(R.id.tv01); this.idcontent = (FrameLayout) findViewById(R.id.id_content); } @Override public void onClick(View view) { int id = 0; if (view == tv02) { id = 1; } else if(view == tv03) { id = 2; } else if (view == tv04) { id = 3; } setSelect(id); }}
代码地址:下载源码
Fragment的生命周期
状态
- 运行状态
- 暂停状态
- 停止状态
- [销毁状态],在API中是没有到这个状态的,因为已经销毁了… :)
Fragment的生命周期
- onAttach():当Fragment被添加到Activity的时候就会被调用,这个方法有且仅会调用一次。
- onCreat(Bundle saveStatus):创建的Fragment的时候会被调用,同理,有且仅调用一次。
- onCreatView():方法如其名字:当创建View的时候:)。每次创建或者是绘制的时候都会回调这个方法的,Fragment将会显示该方法返回的View组件。所以,当我们需要修改View的时候可以在这里修改的:)。
- onActivityCreated():好吧,还能说什么呢,看名字就知道了。当Fragment的老大Activity被调用的时候会调用。
- onStart():启动Fragement的时候会被调用滴~~
- onResume():恢复Fragement的时候这个方法会被回调。
- onPause():暂停的时候这个方法会被回调。
- onStop():停止的时候会被回调。
- onDestoryView();在他要干掉他所包含的View的时候~~调用
- onDestory():干掉自己的时候调用~~当然,有且仅有一次机会
- onDetach():将这个Fragement从Activity中删除替换的时候会回调这个方法。在onDestory后会调用一次onDetach。
Fragment的生命周期:
Activity和Fragment的生命周期的对比:
代码:
启动与退出。至于其他的,就不在这里测试了!
package com.notzuonotdied.test_fragment;import android.content.Context;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Notzuonotdied on 2016/8/21. */public class TestFragment extends Fragment { private String TAG = "Notzuonotdied"; @Override public void onAttach(Context context) { super.onAttach(context); Log.i(TAG , "onAttach"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG , "onCreate"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG , "onCreateView"); return inflater.inflate(R.layout.testfragment01, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.i(TAG , "onActivityCreated"); } @Override public void onStart() { super.onStart(); Log.i(TAG , "onStart"); } @Override public void onResume() { super.onResume(); Log.i(TAG , "onResume"); } @Override public void onPause() { super.onPause(); Log.i(TAG , "onPause"); } @Override public void onStop() { super.onStop(); Log.i(TAG , "onStop"); } @Override public void onDestroyView() { super.onDestroyView(); Log.i(TAG , "onDestroyView"); } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG , "onDestroy"); } @Override public void onDetach() { super.onDetach(); Log.i(TAG , "onDetach"); }}
0 0
- Fragment 介绍
- Fragment介绍
- Fragment介绍
- Fragment介绍
- Fragment介绍
- Fragment介绍:
- Fragment介绍
- Fragment介绍
- 【fragment】Android Fragment 基本介绍
- Android Fragment---概要介绍
- Fragment应用的介绍
- Android Fragment---概要介绍
- android fragment详细介绍
- Android4.0-Fragment介绍
- android fragment详细介绍
- fragment的简单介绍
- Android Fragment 基本介绍
- android fragment详细介绍
- FZU 2180 双向BFS
- 37. Sudoku Solver
- 2003系统配置IIS+CGI环境,装active perl提示安装不了的解决办法
- Conturbatio
- 常用控件之--------UILabel
- Fragment介绍
- ListView复用导致图片错乱及闪烁问题
- 为什么使用dojo?dojo与jquery有什么不同?dojo适合什么开发场景?
- JS常见插件
- 1-HTML元素 属性 格式化
- 38. Count and Say
- 2-HTML5样式、链接和表格
- HTML 排版与标签(五)
- 3-HTML5列表、块和布局