Fragment使用简单示例
Fragment即碎片,使用它可以减少Activity的代码量,降低耦合,使代码更清爽,更易于读懂。它和Activity具有类似的生命周期,连主要使用的方法也一样。
Fragment添加到Activity的FragmetLayout:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");
transaction.commit();
Fragment的切换:
transaction.replace(R.id.fragment_content, Fragment2.getInstance(),"fragmentTag");
添加使用add方法。替换使用replace方法。
下面是简单的实现:
java代码
Activity部分:
package com.example.Activity;import com.example.R;
import com.example.Fragment.Fragment1;
import com.example.Fragment.Fragment2;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends FragmentActivity {private Button btn_qiehuan;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setDefaultFragment(savedInstanceState);}/*** 初始化视图*/private void initView() {btn_qiehuan = (Button)findViewById(R.id.button_qiehuan);viewListen();}/*** 设置默认的Fragment* @param savedInstanceState */private void setDefaultFragment(Bundle savedInstanceState) {if (savedInstanceState==null) {FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//将fragment对象add添加到FrameLayout中。而FrameLayout是在Activity中,所以也即绑定到Activitytransaction.add(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");//添加transaction.commit();}}/*** 切换Fragment*/public void switchFragment(){FragmentManager fm = getSupportFragmentManager();//Fragment管理器FragmentTransaction transaction = fm.beginTransaction();//fragment事务/** 每次添加与切换Fragment都共用一个标志:"fragmentTag",因此通过该标识能获取当前fragment*/Fragment currFragment = fm.findFragmentByTag("fragmentTag");if (currFragment instanceof Fragment1) {//如果当前Fragment 是Fragment1的对象//replace替换transaction.replace(R.id.fragment_content, Fragment2.getInstance(),"fragmentTag");//replace替换}else{transaction.replace(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");//replace替换}transaction.commit();//提交 }/*** 设置监听*/private void viewListen() {btn_qiehuan.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {switchFragment();}});}}
Fragment部分:
Fragment1:
package com.example.Fragment;import com.example.R;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment1 extends Fragment {private static Fragment1 instance;private Fragment1() {}public static Fragment1 getInstance(){if (instance == null) {instance = new Fragment1();}return instance; }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_1, container,false);return rootView;}
}
Fragment2:
package com.example.Fragment;import com.example.R;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment2 extends Fragment {private static Fragment2 instance;private Fragment2() {}public static Fragment2 getInstance(){if (instance == null) {instance = new Fragment2();}return instance; }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_2, container,false);return rootView;}
}
xml布局
activity_main.xml
<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" ><LinearLayout
android:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentTop="true"android:background="#22ff22"android:gravity="center_vertical" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Activity标题栏" /></LinearLayout><Button
android:id="@+id/button_qiehuan"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="切换Fragment" /><FrameLayout
android:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_above="@+id/button_qiehuan"android:layout_below="@+id/titleBar" ></FrameLayout></RelativeLayout>
fragment_1.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="vertical" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Fragment1"android:textSize="25sp" /></LinearLayout>
fragment_2
<?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="wrap_content"android:layout_height="wrap_content"android:text="Fragment2"android:textSize="25sp"/></LinearLayout>
Fragment使用简单示例相关推荐
- 【Android】Fragment的简单笔记
被虐了,做某公司笔试时,发现自己连个Fragment的生命周期都写不详细.平时敲代码,有开发工具的便利,有网上各大神的文章,就算忘了也很容易的可以查到,但当要自己不借助外界,却发现自己似乎对该知识点并 ...
- Android组件化开发简单示例
Android组件化示例代码github地址:https://github.com/respost/ModuleDemo 一.组件化初始模型 1.通过一个简单的android项目初始架构图来了解组件化 ...
- python文本处理实例_Python 文件处理的简单示例
这篇文章主要为大家详细介绍了Python 文件处理的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 相关的AP ...
- python获取mac、计算机id_python 获取本机IP、mac地址、计算机名的简单示例
这篇文章主要为大家详细介绍了python 获取本机IP.mac地址.计算机名的简单示例,具有一定的参考价值,可以用来参考一下. 对python获取本机IP.mac地址.计算机名感兴趣的小伙伴,下面一起 ...
- python简单单元测试示范卷_Python 单元测试的简单示例
这篇文章主要为大家详细介绍了Python 单元测试的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 以前我是不 ...
- python二分法求解_Python使用二分法求平方根的简单示例
这篇文章主要为大家详细介绍了Python使用二分法求平方根的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 使 ...
- Unity 简单示例代码和向导/Unity Aplication Block
Unity 简单示例代码和向导 关于Unity 的说明和下载地址,请访问[微软控制反转和依赖注入容器Unity 1.0发布] http://forum.entlib.com/Default.aspx? ...
- php定时刷新token,PHP 定时任务获取微信access_token的简单示例
搜索热词 感兴趣PHP 定时任务获取微信access_token的简单示例的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧. 最近开发微信公众平台,公众号调用各接口时都需使用access_ ...
- python 搭建的http 动态服务器_Python 创建HTTP服务器的简单示例
这篇文章主要为大家详细介绍了Python 创建HTTP服务器的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! ...
- python简单装饰器_python装饰器的简单示例
这篇文章主要为大家详细介绍了python装饰器的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 装饰器的语法以 ...
最新文章
- python 类属性和对象属性--定义和作用域
- 堆排序原理及其实现(C++)
- Redis常见面试题详解
- android aoa 串口,沁恒股份USB Android AOA转接概述
- SpecFlow的力量
- 2680 Problem F: Coin Game
- python内核_python-在多处理模块中使用所有内核的100%
- 64位CentOS6.5下Eclipse用Java连接mysql
- 卡通人物设计_40个很棒的卡通人物设计教程
- 程序员的百宝箱:提升工作效率的七大神器
- 1466:Girls and Boys:优美的拆散早恋学生?
- IIS部署ASP网站项目详细教程(内部含有子目录)
- 【亲近自然亲子营】 世外桃源”享受野趣,双山邂逅浪“慢”~旅程
- github不再通过验证密码方式push代码
- 摄影_焦点、对焦、对焦点
- 模块“XXX.dll”加载失败
- 玩转微派狼人杀成为高手必看攻略
- 区块链对医疗行业的影响
- 让你的浏览器3D起来
- 合并多个ts文件,处理加密的ts文件
热门文章
- web开发第三方登陆之微信登陆
- java微信素材编辑_Java微信公众平台开发之素材管理(Spring Boot 2.X)
- 计算机组成与原理第三章答,计算机组成与原理第三章答案.doc
- 计算机科学之父――图灵
- 支持向量机(SVM)理解以及在sklearn库中的简单应用
- c语言中short作用,详解C语言中整数(short,int,long)
- Xilinx XC7Z020双核ARM+FPGA开发板试用
- 获取英雄联盟全皮肤(极速版)
- java求100以内的素数
- Character Controller角色控制器组件