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 ...
随机推荐
- 浏览器数据库 IndexedDB 入门
一.概述 随着浏览器的功能不断增强,越来越多的网站开始考虑,将大量数据储存在客户端,这样可以减少从服务器获取数据,直接从本地获取数据. 现有的浏览器数据储存方案,都不适合储存大量数据:Cookie 的 ...
- C. Ehab and a 2-operation task
链接 [https://codeforces.com/contest/1088/problem/C] 题意 n个数,最多n+1操作,要么前i个数加x,要么前i个数对x取余,最后使得严格递增 分析 直接 ...
- 【个人阅读】M1/M2阶段总结
1.以前博客的链接 http://www.cnblogs.com/zyctsl/p/4028006.html http://www.cnblogs.com/zyctsl/p/4094011.html ...
- Daily Scrum NO.9
工作概况 符美潇 昨日完成的工作 1.Daily Scrum.日常会议及日常工作的分配和查收. 2.根据第二小组的要求对数据库表的属性进行修改. 今日工作 1.Daily Scrum.日常会议及日常工 ...
- git学习心得
https://github.com/zhangxinn/test/tree/master 自己虽然在课堂上有认真的听老师讲解如何使用github,包括怎样在线学习,怎样在github上建立自己的仓库 ...
- 第七组团队项目——专业课程资源共享平台——需求分析&原型设计
一.项目目标.定位需求: (1)目标:在教师.学生之间建立一个综合的.全面的.快捷的.高效的免费课程和学习资源共享.交流与推荐的开放性平台,实现多维和动态的推荐与分类检索服务. (2)定位:学生与教师 ...
- Practice2 结对子之“小学四则运算”
开发环境:Eclipse,js,css,html 程序完成的方向: 1.可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号.如: 2*(-4) = ...
- 第三个spring冲刺第2天
今天我们有了计时功能的实现,并且在考虑如何使得计时器美观好看达到我们的要求,对此我们换了不同的背景,时钟框,效果还有待查看.
- Atcoder D - Knapsack 1 (背包)
D - Knapsack 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...
- mongo扩展错误
make: *** [php_mongo.lo] Error 1 Ask Question 0 When I installed the Mongo PHP extension, the foll ...