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 ...
随机推荐
- ./configure,make,make install的作用(转)
这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC ...
- HA(High available)--Heartbeat高可用性集群(双机热备)菜鸟入门级
HA(High available)--Heartbeat高可用性集群(双机热备) 1.理解:两台服务器A和B ,当A提供服务,B闲置待命,当A服务宕机,会自动切换至B机器继续提供服务.当主机恢复 ...
- 复制到剪贴板的JS实现--ZeroClipboard (兼解决IE下兼容问题)
复制到剪贴板的JS实现--ZeroClipboard (兼解决IE下兼容问题) 相信绝大多数人都遇到过这样的功能实现,“复制”或者“复制到剪贴板”这样的功能.但是由于各大浏览器的实现方案不一样,导致几 ...
- SpringMVC学习系列-后记 开启项目的OpenSessionInView
在系列的 SpringMVC学习系列(12) 完结篇 的示例项目中,由于当时考虑到OpenSessionInView会对性能有一定的影响,所以就没有配置项目的OpenSessionInView.在ma ...
- SilverlightERP&CRM源码(可用于开发基于Silverlight的CRM,OA,HR,进销存等)
SilverlightERP系统源代码(支持创建OA.SilverlightCRM.HR.进销存.财务等系统之用) 可用于开发以下系统 SilverlightERP SilverlightCRM Si ...
- linux 下 文件权限和文件主
文件与文件夹的权限和所有者 1.chmod -R 755 file 777 含义与来源: 777含义:分别为:所有者.同组用户.其他用户 7的来源:文件有三种操作模式:读4.写2.执行1,分别值为42 ...
- [HTML5]a标签禁止嵌套使用
a标签内部不可再写a标签,否则会与父a标签解析到同一级.
- Codeforces 723c [贪心][乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一个n和m. 第二行给n个数. 每次操作可以把n个数中的任何一个数替代为别的数,问最少的操作次数使得1.2.3.4.5...m中的数出现的次数 ...
- ASP.NET Page对象各事件执行顺序(转)
很久没写 asp.net 的东西了,search 了一下 page 的事件执行顺序,找到如下的东西,仅仅做记录用 Page.PreInit 在页初始化开始时发生 Page.Init 当服务器控件初始化 ...
- jdbc中c3p0的配置信息
<c3p0-config> <!-- 这是默认配置信息 --> <default-config> <!-- 连接四大参数配置 --> <prope ...