fragment 碎片整理

activity_m1.xml
<?xml version="1.0" encoding="utf-8"?>
<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: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="com.hanqi.music_bofangqi.MActivity1"> <fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:name="com.hanqi.music_bofangqi.MyFragment1"
android:id="@+id/fragment1"
/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.hanqi.music_bofangqi.MyFragment2"
android:id="@+id/fragment2"
/>
</LinearLayout>
MActivity.java
package com.hanqi.music_bofangqi; import android.os.Bundle;
import android.support.v4.app.FragmentActivity; //1-Activity要继承FragmentActivity
public class MActivity1 extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
}
}
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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: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="com.hanqi.music_bofangqi.FragmentActivity"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加Fragment"
android:onClick="bt1_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="移除Fragment"
android:onClick="bt2_onclick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/ll"
android:orientation="horizontal"> </LinearLayout> </LinearLayout>
FragmentActivity.java
package com.hanqi.music_bofangqi; import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View; public class FragmentActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
//碎片的容器
// 1.获得放置fragment的布局管理器
//linearLayout= (LinearLayout)findViewById(R.id.ll);
//2.动态加载fragment
//1)得到管理器
fragmentManager =getSupportFragmentManager();
//2) 得到事务管理器
fragmentTransaction = fragmentManager.beginTransaction();
//3)向容器内放入fragment,参数:1-容器id 2-fragment的实例,并提交
fragmentTransaction.add(R.id.ll,new MyFragment1()).commit();
}
MyFragment2 myFragment2; //动态添加
public void bt1_onclick(View v)
{
fragmentManager =getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//加入回退栈
fragmentTransaction.addToBackStack(null);
//3)向容器内放入fragment,参数:1-容器id 2-fragment的实例,并提交
// replace替换
fragmentTransaction.replace(R.id.ll, myFragment2).commit();
}
public void bt2_onclick(View v)
{
fragmentManager =getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//加入回退栈
fragmentTransaction.addToBackStack(null);
//remove移除
fragmentTransaction.remove(myFragment2).commit();
} }
fragment_my_fragment1.xml
<FrameLayout 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"
tools:context="com.hanqi.music_bofangqi.MyFragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是碎片1"
android:background="#f00"/> </FrameLayout>
fragment_my_fragment2.xml
<FrameLayout 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"
tools:context="com.hanqi.music_bofangqi.MyFragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是碎片2" /> </FrameLayout>
MyFragment1.java
package com.hanqi.music_bofangqi; import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyFragment1 extends Fragment { public MyFragment1()
{
Log.e("TAG","构造碎片"); }
//在Activity显示完成之后调用的
// 设置Fragment的视图
//返回一个显示的View
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("TAG","onCreateView被调用"); return inflater.inflate(R.layout.fragment_my_fragment1, container, false);
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TAG", "onCreate被调用");
} //在Activity创建完成后调用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("TAG", "onActivityCreated被调用");
} @Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("TAG", "onAttach被调用");
} @Override
public void onStart() {
super.onStart();
Log.e("TAG", "onStart被调用");
} @Override
public void onResume() {
super.onResume();
Log.e("TAG", "onResume被调用");
} @Override
public void onPause() {
super.onPause();
Log.e("TAG", "onPause被调用");
} @Override
public void onStop() {
super.onStop();
Log.e("TAG", "onStop被调用");
} @Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "onDestroy被调用");
} @Override
public void onDestroyView() {
super.onDestroyView();
Log.e("TAG", "onDestroyView被调用");
} @Override
public void onDetach() {
super.onDetach();
Log.e("TAG", "onDetach被调用");
}
}
MyFragment2.java
package com.hanqi.music_bofangqi; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class MyFragment2 extends Fragment { public MyFragment2()
{ }
//在Activity显示完成之后调用的
// 设置Fragment的视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { //手动创建视图
TextView textView = new TextView(getActivity()); textView.setText("这是手动生成的内容");
return textView;
//return inflater.inflate(R.layout.fragment_my_fragment2, container, false); } }
fragment 碎片整理的更多相关文章
- 关闭电脑SSD的磁盘碎片整理
小白往往会把机械硬盘时代的习惯带进固态硬盘时代,比如碎片整理.机械硬盘时代砖家最喜欢告诉小白:“系统慢了吧?赶紧碎片整理撒.”小白屁颠屁颠地整理去了.殊不知碎片整理对于SSD来说完全就是种折磨.这种“ ...
- sql索引碎片产生的原理 解决碎片的办法(sql碎片整理)(转)
本文讲述了SQL SERVER中碎片产生的原理,内部碎片和外部碎片的概念.以及解决碎片的办法和填充因子.在数据库中,往往每一个对于某一方面性能增加的功能也会伴随着另一方面性能的减弱.系统的学习数据库知 ...
- 转载:为什么Linux不需要磁盘碎片整理
转载自:www.aqee.net 如果你是个Linux用户,你可能听说过不需要去对你的linux文件系统进行磁盘碎片整理.也许你注意到了,在Liunx安装发布包里没有磁盘碎片整理的工具.为什么会这样? ...
- 为什么Linux不需要碎片整理?
如果你是一个 Linux 用户,你可能会听说 Linux 的文件系统不需要碎片整理.你也可能会注意到 Linux 的发行版本也都没有磁盘碎片整理的功能.这是为什么呢? 要理解为什么 Linux 的文件 ...
- UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版
软件名称:UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:3.25 MB软件等级:整理时 ...
- Defraggler磁盘碎片整理工具,让你的电脑读写速度更快
相信大家都听说过磁盘碎片整理吧,所谓磁盘碎片,通俗的来说,就是指计算机中的各种文件最开始在磁盘中存储的时候地址都是连在一起的,但是随着文件 的多次读写,或者说多次的移动复制等操作,这些文件在磁盘中的地 ...
- 14 Fragment 碎片总结
Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...
- Virtual box中Ubuntu虚拟机磁盘碎片整理和空间清理方法
虚拟机中,随着不断的使用,增加大文件(例如日志,视频和软件版本),虽然在虚拟机中手动删除了,但是虚拟机占用的空间并不会随之减少,需要手动清理一下. 这里介绍一种Virtual box中Ubuntu碎片 ...
- SQL Server索引碎片整理实际操作记录
SQL Server 版本是 2008 R2. 查询数据库索引碎片情况的 SQL 语句(来源): SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName, ind ...
随机推荐
- SSIS 部署到SQL Job
微软 BI 系列随笔 - SSIS 基础 - 部署SQL Job 简介 在之前博客中,讲述了如何实现SSIS的项目部署以及利用SSIS的参数与环境加速部署,参见 微软 BI 系列随笔 - SSIS 基 ...
- Java 二进制与十六进制转换
Java 二进制与十六进制转换 二进制转换十六进制 /** * @description 将二进制转换成16进制 * * @param buf * @return */ public static S ...
- 读CopyOnWriteArrayList有感
除了加锁外,其实还有一种方式可以防止并发修改异常,这就是将读写分离技术(不是数据库上的). 先回顾一下一个常识: 1.JAVA中“=”操作只是将引用和某个对象关联,假如同时有一个线程将引用指向另外一个 ...
- 使用C#访问SQLLite
1.SQLLite如何集成在C#中 2.相关C#与SQLLite资源,及说明 3.简单示例
- js 生成 yyyy-mm-dd 格式的逼格姿势
关于 js 生成 yyyy-mm-dd 格式,往往都会采取手动拼接,一般不愿意为了小功能而去动用 momentjs 之类的插件. ps: 只分享简单方法,网上有 N 多 dateformat 代码,这 ...
- SeedDms 文档管理系统安装
在xampp下安装SeedDms 1.下载seeddms-quickstart-4.3.24.tar.gz,解压出来三个目录 \data\ \pear\ \seeddms-4.3.24\ 我把seed ...
- X-Cart 学习笔记(四)常见操作
目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...
- [转] 解决HttpServletResponse输出的中文乱码问题
首先,response返回有两种,一种是字节流outputstream,一种是字符流printwrite. 申明:这里为了方便起见,所有输出都统一用UTF-8编码. 先说字节流,要输出“中国" ...
- img会在特定的情况下主动的第二次调用MVC的action
当img的src为空,或者不是合法路径的时候,会连续调用MVC的action两次,第二次所传的值为src的值,这常常会导致程序报错. 这不是MVC的issue,而是浏览器的行为,当response f ...
- android 横竖屏切换
解决Android手机 屏幕横竖屏切换 Android中当屏幕横竖屏切换时,Activity的生命周期是重新加载(说明当前的Activity给销毁了,但又重新执行加载),怎么使屏幕横竖屏切换时,当前的 ...