Android Fragment和FragmentActivity区别和用法
Android Fragment是Android4.0以上才有的;而FragmentActivity是为了兼容4.0以下版本的Fragment使用的。
所以如果你想兼容4.0以下Android版本使用Fragment的话,框架Activity需要继承FragmentActivity,FragmentActivity这个类是在android.support.v4.app.FragmentActivity里的。
下面介绍2种用法:
1、继承Activity的。
(这个只针对4.0以上的Android平台使用Fragment)。
框架Activity:
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 不兼容4.0以下模式Fragment
*
* @author tandong
*
*/
public class Mt_Activity extends Activity implements OnClickListener {
private Button btn_first, btn_second;
private Fragment Fragment_first, Fragment_Second;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
initView();
Fragment_Second = new Fragment_Second();
fragmentManager = this.getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second,"second_Fragment");
fragmentTransaction.commit();
}
private void initView() {
btn_first = (Button) this.findViewById(R.id.btn_first);
btn_second = (Button) this.findViewById(R.id.btn_second);
btn_first.setOnClickListener(this);
btn_second.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btn_first:
// 加载不同的Fragment
if (null == Fragment_first) {
Fragment_first = new Fragment_first();
}
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first, "fist_Fragment");
fragmentTransaction.commit();
break;
case R.id.btn_second:
if (null == Fragment_first) {
Fragment_Second = new Fragment_Second();
}
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second, "second_Fragment");
fragmentTransaction.commit();
break;
default:
break;
}
}
}
Fragment代码:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_Second extends Fragment {
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container==null)
return null;
rootView = inflater.inflate(R.layout.fragment_two, container,false);
return rootView;
}
}
2.继承FragmentActivity的(向下兼容4.0以下版本使用Fragment,导入的是android.support.v4包里的内容)
框架Activity:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 兼容4.0以下模式Fragment
*
* @author tandong
*
*/
public class Mt_Activity extends FragmentActivity implements OnClickListener {
private Button btn_first, btn_second;
private Fragment Fragment_first, Fragment_Second;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
initView();
Fragment_first = new Fragment_first();
fragmentManager = this.getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first,"first_Fragment");
fragmentTransaction.commit();
}
private void initView() {
btn_first = (Button) this.findViewById(R.id.btn_first);
btn_second = (Button) this.findViewById(R.id.btn_second);
btn_first.setOnClickListener(this);
btn_second.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btn_first:
// 加载不同的Fragment
if (null == Fragment_first) {
Fragment_first = new Fragment_first();
}
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first, "fist_Fragment");
fragmentTransaction.commit();
break;
case R.id.btn_second:
if (null == Fragment_first) {
Fragment_Second = new Fragment_Second();
}
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second, "second_Fragment");
fragmentTransaction.commit();
break;
default:
break;
}
}
}
Fragment代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_first extends Fragment {
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container==null)
return null;
rootView = inflater.inflate(R.layout.fragment_first, container,false);
return rootView;
}
}
最后再说一句布局:
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/top_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/top_bar_bg"
android:text="按钮一" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/top_bar_bg"
android:text="按钮二" />
</LinearLayout>
<LinearLayout
android:id="@+id/fragment_replace_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/top_bar_layout"
android:background="#ff0000" >
</LinearLayout>
</RelativeLayout>
布局类似这种布局即可,并不是一定非要FrameLayout
Android Fragment和FragmentActivity区别和用法的更多相关文章
- android Fragment和FragmentActivity
MainActivity.java import android.app.AlertDialog; import android.app.Notification; import android.co ...
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- Android Fragment用法知识点的讲解
Android Fragment用法的讲解 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示.Fragment的出现,如微信的额主界面包含多个Fragment,使得微信功能更加简洁明了 ...
- Android Fragment 真正的完全解析
出处: 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~ 本篇博客力求为大家说明Fragmen ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment 解析和使用
Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...
- Android Fragment 你应该知道的一切
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42628537,本文出自:[张鸿洋的博客] 很久以前写过两篇Fragment的介绍 ...
- Android Fragment详解
一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...
- 转 Fragment 和 FragmentActivity的使用
今天学习下 Android中的 Fragment 和 FragmentActivity,因为没有4.0手机,平台是2.3.3 所以我是使用 v4 support 包来进行学习. 要想用Fragment ...
随机推荐
- Microsoft Visual Studio 2013安装及试用
我是在网上下载的vs2013版的安装包,下载的是压缩文件,解压后是2.86GB.安装包下载完成后我们就可以进入安装了. 同时建议最好在互联网连接的情况下安装. 打开下载好的文件,我们要选择.exe可执 ...
- Zookeeper 源码学习(一)环境搭建
前言 最近准备学习 Zookeeper,想从 Zookeeper 开始逐步深入了解各类中间件,学习分布式计算. 下载源码 执行指令,下载代码: git clone https://github.com ...
- Notes of Daily Scrum Meeting(12.23)
今天的团队任务总结如下: 团队成员 今日团队工作 陈少杰 调试网络连接,寻找新的连接方法 王迪 建立搜索的UI界面 金鑫 查阅相关资料,熟悉后台的接口 雷元勇 建立搜索的界面 高孟烨 继续美化界面,熟 ...
- 20135337——Linux实践三:程序破解
程序破解 查看 运行 反汇编,查看汇编码 对反汇编代码进行分析: 在main函数的汇编代码中可以看出程序在调用"scanf"函数请求输入之后,对 [esp+0x1c] 和 [esp ...
- ABP框架用Dapper实现通过SQL访问数据库
ABP的框架(2) - 访问数据库 为了防止不提供原网址的转载,特在这里加上原文链接:http://www.cnblogs.com/skabyy/p/7517397.html 本篇我们实现数据库的 ...
- IP工具类
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...
- An internal error has occurred. Java heap space
http://stackoverflow.com/questions/11001252/running-out-of-heap-space issue: I am having a heap spac ...
- docker发现端口是tcp6的 导致无法访问前端
最近偶尔发现一个比较奇怪的现象,netstat 查看监听的服务端口时,却只显示了 tcp6 的监控, 但是服务明明是可以通过 tcp4 的 ipv4 地址访问的,那为什么没有显示 tcp4 的监听呢? ...
- [转帖]GitHub上整理的一些工具
GitHub上整理的一些工具 技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 inf ...
- centos目录
cd /opt cd /home/lujie cd /etc cd /usr cd /dev cd /bin cd /mnt cd /media cd /tmp