Android Fragment是什么
Fragment是Activity中用户界面的一个行为或者一个部分。你可以在一个单独的Activity上把多个Fragment组合成一个多区域的UI,并且可以在多个Activity中使用。你可以认为Fragment是Activity的一个模块零件,它有自己的生命周期,接收它自己的输入时间,并且可以在Activity运行时添加或者删除。
Fragment的生命周期直接受其宿主Activity的生命周期的影响。例如,一旦Activity被暂停,它里面所有的Fragment也被暂停,一旦Activity被销毁,它里面所有的Fragment也被销毁。
Fragment的使用
第一种方式,主布局中实现,<fragment>标签里面的name属性指向要显示的Fragment类
<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="match_parent"
tools:context=".MainActivity" > <fragment
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight=""
android:name="com.example.fragmentdemo.TitleFragment"
android:id="@+id/titlefragment"
/> <fragment
android:id="@+id/contentfragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight=""
android:name="com.example.fragmentdemo.ContentFragment"
/> </LinearLayout>
新建两个Fragment类 TitleFragment和ContentFragment
public class TitleFragment extends Fragment implements OnClickListener{
private MyMenuListener myMenuListener;
//重写onAttach方法,把TitleFragment依附的宿主mainActivity实现的接口传回给自己
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
myMenuListener=(MyMenuListener) activity;
}
//重写onCreateView 绑定布局给fragment,并且定义fragment当中按钮事件
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.titlelayout,container,false);
Button btn=(Button) view.findViewById(R.id.button1_menu);
btn.setOnClickListener(this);
return view;
}
//声明接口,为了Fragment之间通过它们的宿主mainActivity实现交互
public static interface MyMenuListener{
public void changeValue(String value);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button1_menu:
myMenuListener.changeValue("One");
break;
default:
break;
}
}
}
public class ContentFragment extends Fragment {
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.contentlayout,container,false);
tv=(TextView) view.findViewById(R.id.textView1_content);
return view;
}
//定义该方法,为了在mainActivity中调用
public void changeTextView(String value){
tv.setText(value);
}
}
主代码mainActivity中实现Fragment之间交互显示
public class MainActivity extends Activity implements TitleFragment.MyMenuListener{
TitleFragment titleFragment;
ContentFragment contentFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleFragment=(TitleFragment) getFragmentManager().findFragmentById(R.id.titlefragment);
contentFragment=(ContentFragment) getFragmentManager().findFragmentById(R.id.contentfragment);
}
@Override
public void changeValue(String value) {
// TODO Auto-generated method stub
contentFragment.changeTextView(value);
}
}
第二种方式,代码中添加Fragment,布局文件如下,这里用FrameLaout布局
<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="match_parent"
tools:context=".MainActivity" > <fragment
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight=""
android:name="com.example.fragmentdemo.TitleFragment"
android:id="@+id/titlefragment"
/> <FrameLayout
android:id="@+id/content_layout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight=""
></FrameLayout>
</LinearLayout>
代码中进行动态加载
public class MainActivity2 extends Activity{
ContentFragment contentFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
addContentLayout();
}
private void addContentLayout()
{
FragmentManager fm=getFragmentManager();
//开启一个事务
FragmentTransaction ft=fm.beginTransaction();
contentFragment=new ContentFragment();
ft.add(R.id.content_layout,contentFragment);
ft.commit();
}
}
Android Fragment是什么的更多相关文章
- 【Android自学日记】【转】Android Fragment 真正的完全解析(下)
上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上). 本篇将介绍上篇博客提到的:如何管理Frag ...
- Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...
- Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment使用(一) 基础篇 温故知新
Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...
- Android Fragment应用实战
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- Android Fragment 真正的完全解析(下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...
- Android Fragment
1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...
- Android Fragment完全解析
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...
随机推荐
- video
<div class="index-video-wrapper"> <video autoplay loop poster="img/index-ima ...
- 使用HttpWebrequest对网站进行模拟操作(附登陆百度demo)
// a[href=#viewSource]"); //查看源代码标签 viewSourceArr.attr("title", "查看源代码"); v ...
- Jfianl
http://www.oschina.net/question/257183_149268----------- 添加Handler: me.add(new ContextPathHandler(&q ...
- jfinal路由简单解析
在jfinal中,通过JFinalFilter对所有的类进行过滤. 以下是路由的调用关系(我在调用关系旁边做了标记,会贴出具体的代码和解释): -1- Config: Routes -2- Inter ...
- 【五子棋AI循序渐进】——多线程搜索
关于多线程搜索,有很多方法来实现,很多文章推荐基于MTD(F)的方式.好处不言而喻,不过我的程序中采用的是基于PVS的多线程搜索.实现起来主要是这几个方面问题需要解决: 1.置换表的互斥访问. 2.局 ...
- mui待解决问题
$.plusReady(function () { }); 里面的方法不执行: plusReady仅在5+ App或流应用中会触发 plusReady 参考网址: http://ask.dcloud ...
- UIView画虚线边框
//fatherView加虚线边框 -(void)boundingRectangleForView:(UIView *)fatherView{ CAShapeLayer *borderLayer = ...
- [转]使用Gradle发布Android开源项目到JCenter
转自:http://blog.csdn.net/maosidiaoxian/article/details/43148643 使用Gradle发布Android开源项目到JCenter 分类: G ...
- 下拉列表select显示ng-options
js中如何处理: it-equipment-list-maintenance-create-controller.js 'use strict'; myApp.controller( 'itEquip ...
- PRAGMA AUTONOMOUS_TRANSACTION
转自 http://blog.csdn.net/pan_tian/article/details/7675800 这段时间遇到一个问题,程序里明明插入了一条记录,但在后边的一段Procedure中却查 ...