android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容。右边的内容就是一个fragment,通过点击按键来控制fragment的实现。

工程目录


需要定义三个fragment类,每个类中显示不同的内容,每个fragment对应一个布局文件。将来的fragment会用布局文件来填充,填充好的fragment会被放到framelayout中显示

布局文件


在主布局文件定义按钮和帧布局

<?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:orientation="horizontal"> <LinearLayout
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f1"
android:onClick="click1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f2"
android:onClick="click2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f3"
android:onClick="click3"/>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>

帧布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"> </LinearLayout>

Fragment1类


fragment1类需要继承Fragment,这里注意是adroind类库中的fragment,而不是adroind支持类库中fragment类。

package xidian.dy.com.chujia;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by dy on 2016/8/25.
*/
public class Fragment1 extends Fragment{ @Nullable
@Override
//加载我们的帧布局文件,将要显示的内容来填充fragment
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment1, null);
return v;
}
}

Activity


在Activity中定义帧布局,当点击事件发生的时候切换帧

package xidian.dy.com.chujia;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View; public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void click1(View v){
Fragment1 f1 = new Fragment1();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment, f1);
ft.commit();
} public void click2(View v){
Fragment2 f2 = new Fragment2();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment, f2);
ft.commit();
} public void click3(View v){
Fragment3 f3 = new Fragment3();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment, f3);
ft.commit();
}
}

   显示一个fragment的过程和数据库的事务很类似,首先需要通过FragmentManager来开启一个事务,然后通过replace方法来进行绑定。replace的第一参数的framelayout的ID,第二个参数是具体的fragment,最后进行事务的提交。

android之fragment的使用的更多相关文章

  1. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  2. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  3. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  4. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  5. android之Fragment基础详解(一)

      一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...

  6. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  7. Android使用Fragment定义弹出数字键盘

    fragment主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...

  8. Android ViewPager Fragment使用懒加载提升性能

     Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...

  9. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

随机推荐

  1. 烂泥:通过binlog恢复mysql备份之前的数据

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上一篇文章,我们讲解了如何通过mysql的binlog日志恢复mysql数据库,文章连接为<烂泥:通过binlog恢复mysql数据库>.其 ...

  2. stm32 中断几个库函数实现过程分析

    感谢原文作者:鱼竿的传说,这篇文章写得不错,转载自 http://www.cnblogs.com/chineseboy/archive/2013/03/14/2956782.html 前题: 闭门造车 ...

  3. Java的注解机制——Spring自动装配的实现原理

    http://www.cnblogs.com/Johness/archive/2013/04/17/3026689.html

  4. Linux下监听或绑定(bind)843端口失败

    问题:写了一个程序,尝试在843端口监听,结果在执行bind的时候失败了 原来,系统不允许用户程序在1-1024端口监听,因为他们是知名端口. 解决办法: 换成root用户,即可成功bind.(ubu ...

  5. HRV基础

    Source: Mostly from wiki. Heart rate variability (HRV,心率变异性) is the physiological phenomenon of vari ...

  6. Standard Error of Mean(s.e.m.)

    · 来源:http://www.dxy.cn/bbs/thread/6492633#6492633 6楼: “据我所知,SD( standard deviation )反应的是观测值的变异性,其表示平 ...

  7. MVC HTTP 错误 403.14 - Forbidden

    HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容. 最可能的原因: 没有为请求的 URL 配置默认文档,并且没有在服务器上启用目录浏览. 可尝试的操作: ...

  8. Java设计模式之-----策略模式

    首先,我们来看下策略模式的概念.一般的解释如下:     策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The St ...

  9. Java中HashSet,HashMap和HashTable的区别

    HashMap.HashSet.HashTable之间的区别是Java程序员的一个常见面试题目,在此仅以此博客记录,并深入源代码进行分析: 在分析之前,先将其区别列于下面 1:HashSet底层采用的 ...

  10. Html5 Egret游戏开发 成语大挑战(四)选关界面

    通过前面的开始界面基本上了解了eui的使用方法,可以简单快速的制作一个UI界面,本篇使用第二界面选关界面展示更为难一点的代码控制,来展现关卡地图的内容,请确保素材和资源完整,可以在前面的教程中找到下载 ...