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. 获取当前匹配元素 包括自身的html

    $(".test").prop("outerHTML"); 来自为知笔记(Wiz)

  2. ANDROID模拟器访问本地WEB应用

    一个早上就是想让android的模拟器可以访问到web的应用程序,但是一直是不可以,弄的不知所措. 在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的 ...

  3. Arduino 电平转换 升压 OUTPUT与9V/12V元件通信

    原因 网络上有不少怎么让Arduino的5V电平转换成3.3V电平,从而和工作在3.3V下的芯片相互沟通的教程.但是如果想驱动高于5V电压的芯片,就找不到教程了.因此今天我来介绍一种方式,能让Ardu ...

  4. Arduino开发常见错误

    使用Ethernet时需要指定访问服务器的ip,我用的是本机做服务器.但是有一天重启了路由器,ip地址就变了!程序得跟着改! Arduino突然烧写不了程序:可能是正在运行的程序让arduino死机了 ...

  5. IClassSchemaEdit修改要素类信息

    private void ChangeFeatureClassAliasName(IFeatureClass pFeatureClass, string aliasName) { ISchemaLoc ...

  6. 清除Xcode缓存和存档文件

    XCode4.2    finder中找到   /Users/Library/Developer/Xcode    (注:Library资源库是隐藏的文件夹) 里面有DerivedData和Snaps ...

  7. android 带边框的圆角按钮

    新建buttonstyle.xml 代码如下 <?xml version="1.0" encoding="UTF-8"?> <layer-li ...

  8. 使用RoboCopy 命令

    经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...

  9. poj 3254(状态压缩基础题)

    题意:就是你给一个n行m列的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种植作物,0则不可以种植作物,并且相邻的土地不能同时种植作物,问你有多少种种植方案. 分析:这是我做的第一道状态压缩dp ...

  10. android 深入研究ratingbar自定义

    http://blog.csdn.net/rain_butterfly/article/details/22892879