fragment

1.fragment解释:

  从英文上来说fragment是碎片和片段的意思,这解释的是相当到位的,因为android中的fragment就像是碎片嵌在了Activity当中的,为构造良好的UI起着至关重要的作用。

  fragment和Activity很像,Activity是通过复写onCreate()方法来载入布局文件的,而fragment是通过onCreateView()方法返回一个fragment的布局的。

2.fragment使用前提:

  要使用fragment你的SDK版本必须在11以上。在你的Manifest.xml查看版本。

  我用的是:

 <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <!-- 要大于11才行 -->

3.下面就通过一个简单的例子来说明fragment的使用方法

  MainActivity的class:

 public class MainActivity extends FragmentActivity implements OnClickListener {  //要继承FragmentActivity噢

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.btn1);  //开第一个fragment
Button btn2 = (Button) findViewById(R.id.btn2);  //开第二个fragment
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
System.out.println("第一个button");
FragmentTransaction ft = getSupportFragmentManager() //注意这里是getSupportFragmentManager()不是getFragmentManager() 因为是用了android.support.v4.app不是用了android.support.app
.beginTransaction();
FirstFragment first = new FirstFragment();
ft.replace(R.id.container, first);
ft.commit();
break;
case R.id.btn2:
FragmentTransaction ft2 = getSupportFragmentManager()
.beginTransaction();
SecondFragment second = new SecondFragment();
ft2.replace(R.id.container, second);
ft2.commit();
break;
default:
break; } } }

MianActivity

 

  MainActivity对应的xml文件:

 <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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换至第一个fragment" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换至第二个fragment" />
</LinearLayout>
<!-- 用来装fragment --> <RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout> </LinearLayout>

activity_main.xml

  FirstFragment的class

 public class FirstFragment extends Fragment {

     public FirstFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
// 转向Activity的button
Button btn1 = (Button) view.findViewById(R.id.button1);
// 开第二个fragment
Button btn2 = (Button) view.findViewById(R.id.button2);
btn1.setOnClickListener(new btn1());
btn2.setOnClickListener(new btn2());
return view;
} class btn1 implements OnClickListener { @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getActivity(), SecondActivity.class);
startActivity(intent);
} } class btn2 implements OnClickListener { @Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction(); //是getFragmentManager不是getSupportFragmentManager
SecondFragment second = new SecondFragment();
ft.replace(R.id.container, second);
ft.commit(); } } }

FirstFragment

  

  FirstFragment所引用的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:background="@android:color/darker_gray"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="我是第一个fragment" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开一个activity" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转向第二个fragment" /> </LinearLayout>

first_fragment.xml

  SecondFragment的class

 public class SecondFragment extends Fragment {

     public SecondFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.second_fragment, container, false);
Button btn2 = (Button) view.findViewById(R.id.button111);
btn2.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
FragmentTransaction fm = getFragmentManager()
.beginTransaction();
FirstFragment first = new FirstFragment();
fm.replace(R.id.container, first);
fm.commit(); }
});
return view; //一定要记住放回一个VIEW对象
}
}

SecondFragment

  SecondFragment多引用的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:background="@android:color/white"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="我是第二个个fragment" /> <Button
android:id="@+id/button111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转向第一个fragment" /> </LinearLayout>

second_fragment.xml

  

  SecondActivity的class

 public class SecondActivity extends Activity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
} }

SecondActivity

  SecondActivity对应的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:background="@android:color/white"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="我是第二个个fragment" /> <Button
android:id="@+id/button111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转向第一个fragment" /> </LinearLayout>

second_fragment.xml

Fragment初步了解的更多相关文章

  1. Android Fragment 初步解析

    Fragment经常在我们的开发中见到,但是自我感觉对Fragment的理解还是处于初级的阶段,接下来我将用几篇文章尽量深的解析Fragment 让我们开始吧!!! Fragment的生命周期 Fra ...

  2. fragment初步认识

  3. Alpha第六天

    Alpha第六天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  4. Alpha冲刺——第六天

    Alpha第六天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  5. Mnesia动态添加节点杂记

    FAQ List: 1. 如果动态的添加一个节点到Mnesia cluster中 2. 如何动态的从mnesia cluster中删除一个节点 3. 在一个节点上演示将当前已有的表格分片fragmen ...

  6. Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理

    Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...

  7. Fragment详解之三——管理Fragment(1)

    相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...

  8. Android之Fragment学习总结(1)

    对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...

  9. 管理Fragment

    转载原地址:http://blog.csdn.net/harvic880925/article/details/44927375 相关文章: 1.<Fragment详解之一——概述>2.& ...

随机推荐

  1. Ant编译环境

    使用ant编译有关于C compiler(cc task)的时候,需要确认vs的环境有无配置, 否则会遇到形形色色的问题,比如各种 vs编译工具找不到的错误, 一个比较简单的解决方案,就是在执行ant ...

  2. Linux VPS 基本命令

    我们Linux VPS用命令才能管理他,我们来罗列一些基本和简单的Linux的命令 1.lsls / 查看根目录ls -a / 查看根目录下所要文件,包括隐藏文件ls -l / 详细列出目录下文件的权 ...

  3. 更新Code First生成的数据库

    1,首次访问时会自动生成数据库 2,某个Model增加一个字段后,再次访问会报,数据库不是最新 操作 1,Enable-Migrations 注意选择Default project为Star.Core ...

  4. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.4. 安装JDK

    2.4.安装JDK 2.4.1.准备JDK 在百度搜索:JDK下载 2.4.2.上传JDK put E:\软件安装文件\jdk-8u11-linux-x64.rpm /home/linuxrac1/D ...

  5. delete之后千万要记得将指针置空,即勿重复delete!!!

    下面这段代码有什么问题吗?(Arduino上运行) class C{ public: C(){ ptr = ]; } ~C(){ if(ptr!=NULL)delete [] ptr; } void ...

  6. Linux下停用和启用用户帐号

    有时候某个用户不乖,但是还不想删除他的帐号只是想给他点儿颜色看看,或者出于某种权限管理不想让他/她使用这个帐号,那么冻结帐号就是最好的方法了,linux中的帐号密码保存在/etc/shadow文件里面 ...

  7. [Everyday Mathematics]20150225

    设 $f:\bbR\to\bbR$ 二次可微, 适合 $f(0)=0$. 试证: $$\bex \exists\ \xi\in\sex{-\frac{\pi}{2},\frac{\pi}{2}},\s ...

  8. [C#搜片神器] 之P2P中DHT网络爬虫原理

    继续接着上一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 昨天由于开源的时候没有注意运行环境,直接没有考虑下载BT种子文件时生成子文件夹,可能导致有的朋友运行 ...

  9. TextView------文字底部或者中间加横线

    promotionLinkText = (TextView) this .findViewById(R.id. text_promotion_link ); 中间加横线 promotionLinkTe ...

  10. java 学习资料

    最近一直在从java基础开始学习java,感觉个人学习思路还是比较混乱的. 看到Java工程师成神之路中介绍了需要学习的知识点,并在<成神之路系列文章>中分章分节的开始介绍其中所有的知识点 ...