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. USACO 2014 Open Silver Fairphoto

    这道题只是银牌组的第一题而我就写了 3K 的代码.唉. Description - 问题描述 FJ's N cows (2 <= N <= 100,000) are standing at ...

  2. JAVA虚拟机内存分配与回收机制

    Java虚拟机(Java Virtual Machine) 简称JVM Java虚拟机是一个想象中的机器,在实际的计算机上通过软件模拟来实现.Java虚拟机有自己想象中的硬件,如处理器.堆栈.寄存器等 ...

  3. css3 :nth-child 常用用法

    前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”. 下面我将用几个典型的实例来给大家讲解:nth-child的实际 ...

  4. 【转】使用Xcode中的iOS SDK给iphone开发出第一个App程序

    之前已经折腾过用Xcode开发OS X的程序了,现在继续折腾,用iOS SDK开发移动设备(iphone/ipad/ipod touch)的程序. 1.从iOS Developer Library中找 ...

  5. LruCache--远程图片获取与本地缓存

    Class Overview A cache that holds strong references to a limited number of values. Each time a value ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. no symbol version for module_layout

    内核模块编译helloworld: no symbol version for module_layout, 尝试各种解决办法, 都没搞定, 版本也是对的. dmesg提示no symbol vers ...

  8. 认识Agile,Scrum和DevOps

    If everything's under control you are going too slow. 当今的开发,要求faster and faster.所以我们要Agile,become Ag ...

  9. Linux-sed用法

    本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命 ...

  10. 项目 erlang启动时死循环

    机子里的otp是新装的 看了一下main 是在util:ensure_started一堆app的时候死讯了, 按照顺序是sasl crypto asn1 public_key ssl 发现是publi ...