Fragment(1)-简单实现
来源:互联网 发布:java缓存过程面试 编辑:程序博客网 时间:2024/06/16 06:40
关于fragment ,
1.是一个区域的UI,可以绑定自己的Layout。当然,他也可以不在界面中显示,而在后台工作。
2.我们可以在一个Activity里加入多个Fragment,也可以多个Activity重用一个Fragment。
3.它拥有自己的生命周期,同时被它所寄存的Activity的生命周期所影响。当Activity销毁,Fragment也被销毁,当Activity暂停,Fragment也暂停
4.Activity正运行时,我们可以对Fragment进行添加,删除,替换等操作
简单实现
1.在main_activity.xml中添加Fragment的layout,跳出提示框:建立继承自Fragment的子类,
选择Create New
2.在类中添加必要Fragment必要的生命周期方法
OnCreateView:在Fragment第一次绘制用户界面,调用此方法,传入它所对应的layout
OnCreate:创建Fragment调用此方法
OnPause:用户离开调用
这里我只是简单地实现OnCreateView方法,将Layout绑定
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first,container,false); }
ps:代码
first.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment" /></FrameLayout>activity_main.xml
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fiest.MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.example.fiest.first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="88dp" android:layout_marginTop="172dp" android:layout_toRightOf="@+id/textView1" /></RelativeLayout>First类:
package com.example.fiest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class first extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first,container,false); } }
0 0
- Fragment(1)-简单实现
- Android-Fragment(1)简单实现
- viewpage+fragment的简单实现
- 简单实现Fragment回退栈效果
- Android Fragment学习笔记(1)———简单Fragment实现
- 通过Fragment实现简单的上下文菜单
- 使用Fragment实现简单的数据交互
- RadioGroup+Fragment 实现简单选项卡切换
- viewpager+Fragment实现简单滑动切换效果
- Android安卓Fragment简单实现
- SmartTabLayout+PtrFrameLayout+ViewPager+Fragment简单实现
- Android中Viewpage+Fragment的简单实现
- TabHost配合fragment的简单框架实现
- RadioButton+Fragment实现简单主页面布局
- Android Fragment懒加载简单实现
- Fragment实现懒加载以及简单封装
- 使用简单的fragment实现左侧导航
- Android Fragment懒加载简单实现
- unity摄像机跟随的脚本
- Java设计模式--观察者模式
- JamesMusic浅读--------3,欢迎界面。
- Linux关机和重启命令总结
- python 学习2
- Fragment(1)-简单实现
- sys/types.h和fcntl.h的作用
- 精确定时调度模块开发设计
- Android之值得开发者(安卓)必备的十八大开发资源
- python记录
- 程序是怎样跑起来的-第11章 硬件控制方法
- android TextView 如何动态获取宽度
- android开发 从fragment中通过控件跳到另一个activity的实现
- LeetCode-147.Insertion Sort List