fragment入门

① 创建一个类继承Fragment 重写oncreateView方法
public class FirstFragment extends Fragment {

	@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflater就是把一个xml文件打气成一个view 而且返回值本就是view
View view = inflater.inflate(R.layout.fragment_first, null);
return view;
}
}
 
②在布局文件中声明相应的fragment 注意 fragment节点首字母小写
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.itheima.fragment.FirstFragment" <!--指向要显示的fragment 要用fragment的全类名 -->
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.itheima.fragment.SecondFragment"
android:id="@+id/viewer"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
 
tips 获取全类名
③ 创建fragment对应的布局
<?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" >
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个fragment"/> </RelativeLayout>
 


2 动态替换fragment

①创建Fragment 写一个类继承 Fragment 重写oncreateView  
② 给fragment创建布局 在oncreateView  把布局转换成view对象
③在用到fragment的activity中 获得FragmentManager
④ 通过FragmentManager 开启一个FragmentTransaction
⑤通过transaction的replace方法 把fragment对象 添加到activity中的一个viewgroup里
⑥ commit提交

activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <LinearLayout
android:id="@+id/ll_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </RelativeLayout>
 

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Point outSize = new Point();
//新api 获取屏幕宽高 传入 Point 这行代码执行之后 point中就保存了 屏幕的宽高
getWindowManager().getDefaultDisplay().getSize(outSize);
int width = outSize.x;
int height = outSize.y; //获取FragmentMananger fragment管理器
FragmentManager manager = getFragmentManager();
//通过FragmentManager 开启一个fragment的事务
FragmentTransaction transaction = manager.beginTransaction(); if(height>width){
System.out.println("竖屏");
//android.R 系统定义好的资源id
transaction.replace(R.id.ll_layout, new FstFragment());
}else{
System.out.println("横屏");
transaction.replace(R.id.ll_layout, new SecondFragment());
}
//记住一定要commit提交 否则没效果
transaction.commit();
}
 
 
 
android.R.id.content  android系统预定义了一系列的资源id  
通过android.R.id.content 实际上找到了系统创建的framelayout  这个framelayout的id 就是content


3 使用fragment创建一个选项卡页面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <RelativeLayout
android:id="@+id/rl_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</RelativeLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_wx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/weixin_pressed"
/>
<ImageView
android:id="@+id/iv_contact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/contact_list_normal"/>
<ImageView
android:id="@+id/iv_find"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/find_normal"/>
<ImageView
android:id="@+id/iv_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/profile_normal"/>
</LinearLayout> </LinearLayout>
 
public class MainActivity extends Activity implements OnClickListener {

	private ImageView iv_wx;
private ImageView iv_contact;
private ImageView iv_find;
private ImageView iv_me;
private WxFragment wxFragment;
private FindFragment findFragment;
private ContactFragment contactFragment;
private MEFragment meFragment; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到底部的四个按钮
iv_wx = (ImageView) findViewById(R.id.iv_wx);
iv_contact = (ImageView) findViewById(R.id.iv_contact);
iv_find = (ImageView) findViewById(R.id.iv_find);
iv_me = (ImageView) findViewById(R.id.iv_me); iv_contact.setOnClickListener(this);
iv_find.setOnClickListener(this);
iv_me.setOnClickListener(this);
iv_wx.setOnClickListener(this); //用代码的方式点一下按钮
iv_wx.performClick();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
//把所有图片设置为初始状态
clearImage();
//获得fragmentmanager
FragmentManager manager = getFragmentManager();
//开启fragment事务
FragmentTransaction transaction = manager.beginTransaction();
switch (v.getId()) {
case R.id.iv_wx:
if(wxFragment == null){
wxFragment = new WxFragment();
}
transaction.replace(R.id.rl_layout, wxFragment);
iv_wx.setImageResource(R.drawable.weixin_pressed);
break;
case R.id.iv_contact:
if(contactFragment == null){
contactFragment = new ContactFragment();
}
transaction.replace(R.id.rl_layout, contactFragment);
iv_contact.setImageResource(R.drawable.contact_list_pressed);
break;
case R.id.iv_find:
if(findFragment==null){
findFragment = new FindFragment();
}
transaction.replace(R.id.rl_layout, findFragment);
iv_find.setImageResource(R.drawable.find_pressed);
break;
case R.id.iv_me:
if(meFragment==null){
meFragment = new MEFragment();
}
transaction.replace(R.id.rl_layout, meFragment);
iv_me.setImageResource(R.drawable.profile_pressed);
break;
}
//提交
transaction.commit();
} //把所有的图标设置为初始状态
private void clearImage(){
iv_contact.setImageResource(R.drawable.contact_list_normal);
iv_find.setImageResource(R.drawable.find_normal);
iv_me.setImageResource(R.drawable.profile_normal);
iv_wx.setImageResource(R.drawable.weixin_normal);
}
}
 


4 使用fragment兼容低版本的写法

 
跟fragment相关的所有的代码都要导入 android.support.v4.app. 这个包下的内容
注意 获得FragmentManager区别 想用这个方法 要让当前的activity继承 FragmentActivity
import android.support.v4.app.FragmentActivity;  //导包要导入support包中的fragment相关的类
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu; //必须继承
public class MainActivity extends FragmentActivity {
 
 
 FragmentManager fragmentManager = getSupportFragmentManager();
 
具体使用哪个根据项目情况
如果是老项目 没得选 只能跟别人保持一致
 
如果是新项目 可以选择 不用support包的 用android系统自带的fragment
注意 fragment中点击事件 不用采用第四总方式(在xml中声明onclick属性)
要用代码的方式 setonClickListener
如果非得要用onclick属性 对应的方法要写在 fragment加入到的activity中(不推荐)
 


5 fragment的生命周期
onCreateView 必须重写 通过这个方法加载界面 做初始化的操作
可选的
onDestory 如果fragment占用了某些资源 在onDestory 中要释放占用的资源
onstop/onPause 可以做数据保存的工作
public class FirstFragment extends Fragment {

	//这个方法运行 这个fragment就跟activity建立了练习
//attach 依附 粘
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
System.out.println("onAttach");
//获得绑定的activity的引用
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate");
//创建fragment,并初始化
} //返回一个fragmentUI
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, null);
System.out.println("onCreateView");
return view;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("onActivityCreated");
//当activity和fragment都被创建以后才会调用
//当同时需要activity和fragment中的控件时,可在此方法中调用
} @Override
public void onStart() {
super.onStart();
System.out.println("onstart");
}
@Override
public void onResume() {
super.onResume();
System.out.println("onResume");
} @Override
public void onPause() {
super.onPause();
System.out.println("onPause");
} @Override
public void onStop() {
super.onStop();
System.out.println("onStop");
} @Override
public void onDestroyView() {
super.onDestroyView();
System.out.println("onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("onDestroy");
} @Override
public void onDetach() {
super.onDetach();
System.out.println("onDetach");
}
}
 

 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

 
 
 
 

fragment基础 fragment生命周期 兼容低版本的更多相关文章

  1. [转]AppCompat 22.1,Goole暴走,MD全面兼容低版本

    AppCompat 22.1,Goole暴走,MD全面兼容低版本 分类: Android2015-04-24 09:48 1354人阅读 评论(0) 收藏 举报 android   目录(?)[+] ...

  2. android 在使用ViewAnimationUtils.createCircularReveal()无法兼容低版本的情况下,另行实现圆形scale动画

    ViewAnimationUtils.createCircularReveal()的简介: ViewAnimationUtils.createCircularReveal()是安卓5.0才引入的,快速 ...

  3. 模拟实现兼容低版本IE浏览器的原生bind()函数功能

    模拟实现兼容低版本IE浏览器的原生bind()函数功能: 代码如下: if(!Function.prototype.bind){   Function.prototype.bind=function( ...

  4. Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案

    Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案 解决方式:安装 "babel-polyfill" 即可. 命令:npm install --save-dev ...

  5. Fundebug前端JavaScript插件更新至1.8.0,兼容低版本的Android浏览器

    摘要: 兼容低版本Android浏览器,请大家及时更新. Fundebug前端BUG监控服务 Fundebug是专业的程序BUG监控平台,我们JavaScript插件可以提供全方位的BUG监控,可以帮 ...

  6. 兼容低版本IE浏览器的一些心得体会(持续更新)

    前言: 近期工作中,突然被要求改别人的代码,其中有一项就是兼容IE低版本浏览器,所以优雅降级吧. 我相信兼容低版本IE是许多前端开发的噩梦,尤其是改别人写的代码,更是痛不欲生. 本文将介绍一些本人兼容 ...

  7. 使用fragment兼容低版本的写法

      [1]定义fragment继承V4包中的Fragment    [2]定义的activity要继承v4包中的FragmentActivity   [3]通过这个方法getSupportFragme ...

  8. Android Fragment详解(二):Fragment创建及其生命周期

    Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...

  9. Android开发 - Fragment与Activity生命周期比较

    1. Fragment的生命周期 见下图 2. 与Activity生命周期的对比 见下图 3. 代码场景演示实例 切换到该Fragment: AppListFragment(7649): onAtta ...

随机推荐

  1. 肯德基收银系统java

    参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++或Java或Python结合设计模式(2种以上)至少实现系统的以下功能: 1.正常餐品结算和找零. 2.基本套餐结算和找零. 3.使用优惠 ...

  2. 一键安装本地yum仓库脚本

    #!/bin/bash#By:zhaocheng#Date:2019-01-18#Version v1 [ -d /media/cdrom ] || mkdir /media/cdrom[ -d /m ...

  3. eas之设置编辑界面分录的某一列不可编辑

    KDTEntrys.getColumn(“xx”).getStayAttributes().setlokced(true);

  4. win10家庭版转专业版并激活

    之前重装win10的时候没注意,不小心装成家庭版. 本以为家庭版也没什么,后来发现这对程序员来说造成致命打击. 在系统信息页面底部点击“更改密匙”,输入win10升级产品密匙:VK7JG-NPHTM- ...

  5. win7的目录和vbox的共享,linux中没有权限打开

    来自于 http://www.cnblogs.com/usegear/p/5120427.html win7的目录由vbox共享是个老话题.稳拿网上很多介绍. 在linux中通过文件夹不能打开,说没有 ...

  6. Visifire实现统计图

    /// <summary> /// 柱形图 /// </summary> public static Chart InitCompareChart(string fundNam ...

  7. 混乱的URL编码-C#-JavaScript

    文章地址 前后端传值的时候,有时候总是遇到乱码不知道怎么解决,这个还是要分析下各个语言的编码格式 JavaScript中编码有三种方法:escape.encodeURI.encodeURICompon ...

  8. ZEAL--可以查看所有软件API的软件

    windows平台上最强大的可以查看所有API的软件,从此再也不用去各个网站上扒了,而且有时还有Greate Wall 点我下载

  9. Java中的事务

    Java中的事务 学习了:https://www.cnblogs.com/chengpeng15/p/5802930.html 膜拜一下 org 分为三类:jdbc事务.jta事务.容器事务:

  10. Notepad++ 设置执行 lua 和 python

    Notepad++ 设置执行 lua 和 python 一.设置 run -> 设置 cmd /k lua "$(FULL_CURRENT_PATH)" & PAUS ...