PS:Fragment简介

Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!

下面是效果图,

生命周期图:

加载fragment1--->点击导航点fragment2(扳子)--->点击home键--->回到程序--->点击返回键(退出)。

1:创建Fragment1,及xml文件。因为和Fragment2一样,这里就写一个了

package day1.cn.frag;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import day1.cn.ceshi001.R; public class Fragment1 extends Fragment {
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("Fragment", "onAttach: 11111");
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Fragment", "onCreate: 11111");
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1,container,false);
Log.e("Fragment", "onCreateView: 11111");
return view;
} @Override
public void onStart() {
super.onStart();
Log.e("Fragment", "onStart: 11111");
} @Override
public void onResume() {
super.onResume();
Log.e("Fragment", "onResume: 11111");
} @Override
public void onPause() {
super.onPause();
Log.e("Fragment", "onPause: 11111");
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("Fragment", "onActivityCreated: 11111");
} @Override
public void onStop() {
super.onStop();
Log.e("Fragment", "onStop: 11111");
} @Override
public void onDestroyView() {
super.onDestroyView();
Log.e("Fragment", "onDestroyView: 11111");
} @Override
public void onDestroy() {
super.onDestroy();
Log.e("Fragment", "onDestroy: 11111");
} @Override
public void onDetach() {
super.onDetach();
Log.e("Fragment", "onDetach: 11111");
} @Override
public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
super.onInflate(context, attrs, savedInstanceState);
Log.e("Fragment", "onInflate: 11111"); } }

fragment.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:text="1111111111"
android:gravity="center"/> </LinearLayout>

2:MainActivity.java 及xml文件

(1)fragment开启事物,用的都是import android.app.Fragment;
import android.app.FragmentTransaction;并不是v4包下的。

这里用的是add和hide,,没用replace。

 public void hideFrag(Fragment f1){
FragmentTransaction ft=getFragmentManager().beginTransaction();
if(f1!=null&& f1.isAdded()){
ft.hide(f1); }
ft.commit();
} public void addFrag(Fragment f1) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (f1 != null && !f1.isAdded()) {
ft.add(R.id.id_contentlayout, f1); }
ft.commit();
getFragmentManager().beginTransaction().show(f1).commit();
}
public void hideAllFrag(){
hideFrag(f1);
hideFrag(f2);
}

(2)总代码:

package day1.cn.ceshi001;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout; import day1.cn.frag.Fragment1;
import day1.cn.frag.Fragment2; public class FragmentActivity1 extends AppCompatActivity implements View.OnClickListener { private Fragment1 f1;
private Fragment2 f2;
private LinearLayout l1;
private LinearLayout l2; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout); l1 = (LinearLayout) findViewById(R.id.id_L1);
l2 = (LinearLayout) findViewById(R.id.id_L2); l1.setOnClickListener(this);
l2.setOnClickListener(this);
init(); } private void init() {
hideAllFrag();//首先隐藏全部fragment
if(f1==null){
f1 = new Fragment1();
}
addFrag(f1);
} public void hideFrag(Fragment f1){
//开启事务
FragmentTransaction ft=getFragmentManager().beginTransaction();
//如果不为null 并且已经添加过了,就隐藏掉。
if(f1!=null&& f1.isAdded()){
ft.hide(f1); }
//提交
ft.commit();
} public void addFrag(Fragment f1) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (f1 != null && !f1.isAdded()) {
//添加到指定布局
ft.add(R.id.id_contentlayout, f1); }
ft.commit();
getFragmentManager().beginTransaction().show(f1).commit();//显示所添加的fragment
}
public void hideAllFrag(){
hideFrag(f1);
hideFrag(f2);
} @Override
public void onClick(View v) {
hideAllFrag();
switch (v.getId()){
case R.id.id_L1:
if(f1==null){
f1 = new Fragment1();
}
addFrag(f1); //更改点击后的导航布局背景颜色。
l1.setBackgroundColor(Color.rgb(228,228,228));
l2.setBackgroundColor(Color.WHITE);
break;
case R.id.id_L2:
if (f2 == null) {
f2 = new Fragment2();
}
addFrag(f2);
l2.setBackgroundColor(Color.rgb(228,228,228));
l1.setBackgroundColor(Color.WHITE);
break;
}
}
}

fragment_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_contentlayout"
android:layout_above="@+id/include"> </LinearLayout>
<include android:id="@+id/include"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="60dp"
layout="@layout/bottom"
></include> </RelativeLayout>

底部导航:bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/id_L1"
android:layout_width="0dp"
android:layout_height="55dp"
android:layout_weight="1"
android:background="#e4e4e4"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="@drawable/img2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="首页"/>
</LinearLayout>
<LinearLayout
android:id="@+id/id_L2"
android:layout_width="0dp"
android:layout_height="55dp"
android:layout_weight="1" android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="@drawable/img1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="扳子"/>
</LinearLayout>
</LinearLayout>

完。

Fragment生命周期及实现点击导航图片切换fragment,Demo的更多相关文章

  1. Fragment生命周期及在viewpager中的生命周期

    简介 本篇博客主要从一下三个方面介绍fragement的生命周期 1.fragment的生命周期及与Activity的生命周期的比较 2.FrameLayou布局添加.替换Fragment时fragm ...

  2. Android中Fragment生命周期和基本用法

    1.基本概念 1. Fragment是什么? Fragment是可以让你的app纵享丝滑的设计,如果你的app想在现在基础上性能大幅度提高,并且占用内存降低,同样的界面Activity占用内存比Fra ...

  3. Android Fragment 生命周期及其API使用(建议使用自定义View替换Fragment)

    我为什么不主张使用Fragment Fragment:( Fragment就相当于一个有生命周期的View,它的生命周期被所在的Activity的生命周期管理 ) 生命周期回调说明: onAttach ...

  4. Android Studio 单刷《第一行代码》系列 06 —— Fragment 生命周期

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  5. 关于FragmentManager动态管理Fragment时Fragment生命周期的探究

    Fragment是Android中的重要组件,在Android 3.0的时候添加进来. 关于Fragment的生命周期,我相信了解过的开发人员都应该把以下方法脱口而出:onAttach, onCrea ...

  6. Fragment(四)Fragment生命周期分析(转)

    Fragment(四)Fragment生命周期分析 转载请注明:http://blog.csdn.net/liaoqianchuan00/article/details/24271607   例子一 ...

  7. Android Fragment 生命周期及其正确使用(建议使用自定义View替换Fragment)

    使用Fragment 官方例子中显示: 例如:一个学生Fragment,需要传入studentId,进行http请求显示,那么setArguments后防止杀掉Fragment后,参数为0,显示不了数 ...

  8. 【Android开发】之Fragment生命周期

    上一篇博客我们讲到了,Fragment的基本使用,相信大家都已经了解怎么去使用了.如果还有不懂得同学可以去看一下,传送门.现在我们来讲解一下Fragment的生命周期. 一.Fragment的事务 再 ...

  9. Fragment 生命周期 事务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. Python随笔------初探

    今年的双十一刚刚才过去,大多数人主要就是抢购商品,可能他们现在已经收到了他们夜以继日抢购的商品.然而对于我们做技术的,特别是做互联网技术的,我相信肯定都被双十一那天的许多技术震撼到了吧.云计算.分压式 ...

  2. sklearn 中 make_blobs模块使用

    sklearn.datasets.make_blobs(n_samples=100, n_features=2, centers=3, cluster_std=1.0, center_box=(-10 ...

  3. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  4. Python--Pycharm backup_ver1.py 控制台一直Backup FAILED

    1.windows不自带zip,需自行安装,http://gnuwin32.sourceforge.net/packages/zip.htm 2.安装后,要配置环境变量:PATH 3.简明Python ...

  5. GDAL编译

    使用cmd命令行编译 1.首先在“开始菜单\所有程序\Microsoft Visual Studio 2008\Visual Studio Tools\ Visual Studio 2008命令提示” ...

  6. 2016年7月微软MVP申请開始了!

    2016年7月微软MVP申请開始了! CSDN与微软合作,长期为用户提供申请"微软最有价值专家"的平台.希望有兴趣.资历的朋友以及正在朝这个方向努力的朋友能够积极參与. 2016年 ...

  7. java 可变參数

    我们在某些特定的需求环境下,可能要对某一个方法中的參数进行一些操作,并且这些方法中的參数是不规定的,那么问题来了,我们该怎么办呢? java事实上就为我们考虑了这样的情况,那就是使用可变參数 可变參数 ...

  8. webAPP踩坑记录

    最近公司突然给我们下了一个任务  一个星期要做出一个系统网站 外加手机app   2个同事负责 web开发  我负责手机app 的开发 今天终于初级版本做完了,记录一下手机app踩过的坑与优化之路 用 ...

  9. python抓取历年特码开奖记录

    背景: 小时候,有种游戏,两个主人公:白XX和曾XX,每个家庭把他俩像活菩萨一样供着,供他们吃,供他们穿 做生意的老板为了这两位活菩萨,关门大吉 农民为了这两位活菩萨卖牛卖田变卖家产 做官的为了这两位 ...

  10. 通过第三方工具体验Microsoft Graph

    作者:陈希章 发表于 2017年3月22日 上一篇文章我介绍了如何利用官方提供的Graph 浏览器快速体验Microsoft Graph强大功能,这是极好的起点.官方的Graph浏览器力图用最简单的方 ...