android之fragment的使用
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的使用的更多相关文章
- Android:Activity+Fragment及它们之间的数据交换.
Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- android之Fragment基础详解(一)
一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- Android使用Fragment定义弹出数字键盘
fragment主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...
- Android ViewPager Fragment使用懒加载提升性能
Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...
- 33.Android之Fragment学习
Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...
随机推荐
- java容器(java编程思想第四版-读书笔记)
容器类库图 List(interface) 次序是List最重要的特点:它保证维护元素特定的顺序.List为Collection添加了许多方法,使得能够向List中间插入与移除元素.(这只推荐L ...
- LeetCode #329. Longest Increasing Path in a Matrix
题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ...
- Eclipse和PyDev搭建完美Python开发环境(Windows篇)
目录安装Pythonpython for eclipse插件安装配置PyDev插件测试 安装Python从网站上下载最新的版本,从http://python.org/download/下载.安装过程与 ...
- Facebook的体系结构分析---外文转载
Facebook的体系结构分析---外文转载 From various readings and conversations I had, my understanding of Facebook's ...
- 新手必学的java报表开发工具FineReport实用技巧
1.在制作模板时,如何将报表中的值传递到超链接网页呢? 在项目中以frame方法把F1.CPT放到项目的页面中,对F1.CPT做网络报表超链接F2.CPT,然后在F2.cpt页面中,做个超链接的网页, ...
- 准备使用 Office 365 中国版--域名
无论是购买还是试用Office 365,每个Office 365用户都会有一个唯一的ID.在中国版的Office 365中,这个ID是一个以“.partner.onmschina.cn”结尾的域名.既 ...
- Book LIst
Go ahead. Linux APUE Linux Kernel Development 鸟哥的linux私房菜 基础篇 鸟哥的linux私房菜 服务器篇 Network Computer Netw ...
- 跟我一起写 Makefile
转自 陈皓 的博客:http://blog.csdn.net/haoel/article/details/2886 1. 概述 2. 关于程序的编译和链接 3. Makefile 介绍 4. Make ...
- java 22 - 20 多线程之线程池
程序启动一个新线程成本是比较高的,因为它涉及到要与操作系统进行交互. 而使用线程池可以很好的提高性能,尤其是当程序中要创建大量生存期很短的线程时,更应该考虑使用线程池. 线程池里的每一个线程代码结束后 ...
- gzip: stdout: No space left on device问题的解决
一.问题描述 最近安装了ubuntu14.04,并在ubuntu14.04中编译了一次内核.这之后大部分情况下用sudo apt-get install 安装应用都会出现“gzip: stdout: ...
