Fragment的使用简介

本人新手,看书和别的大神代码总结出一点点使用方法,和大家分享下。

1.Fragment用法简介
Android3.0引入了Fragment,引入Fragment的初衷是为了适应大屏幕的平板电脑,由于平板电脑屏幕更大,因此能够容纳更多的UI组件,而这些UI组件存在这交互关系。Fragment简化了对大屏幕UI的设计,它不需要开发者管理组件包含关系的复杂变化。Fragment必须嵌入Activity中使用,因此Fragment也有自己的生命周期,而且受它所在的Activity影响。
与创建Activity类似,开发者实现Fragment必须继承Fragment基类。通常来说,创建Fragment需要实现如下三个方法:
onCreate():系统创建Fragement对象后回调该方法。
onCreateView():当Fragment绘制界面的时候调用该方法。
onPause():当用户离开该Fragment是会调用该方法。
下面通过Fragment实现一个图片浏览器。
三张图片,那么我们建三个Fragment来更新Activity界面:
第一个:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FragmentOne extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.one, container, false);
((ImageView)rootView.findViewById(R.id.one)).setImageResource(R.drawable.tiger);
return rootView;
}
}
第二个:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FragementTwo extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.one, container, false);
((ImageView)rootView.findViewById(R.id.two)).setImageResource(R.drawable.cat);
return rootView;
}
}
第三个:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FragmentThree extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.one, container, false);
((ImageView)rootView.findViewById(R.id.three)).setImageResource(R.drawable.wolf);
return rootView;
}
}
每个Fragment的布局是一样的:
<?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="vertical" >
<ImageView
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>

</LinearLayout>
然后就是MainActicity的代码:
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button shownext=(Button) findViewById(R.id.shownext);
FragmentOne one=new FragmentOne();
FragementTwo two=new FragementTwo();
FragmentThree three=new FragmentThree();
final Fragment[] fragment=new Fragment[]{
one,two,three
};
shownext.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getFragmentManager().beginTransaction().replace(R.id.fragment_show, fragment[(i++)%3]).commit();
}
});
}
}
通过监听这个按钮,来更换Fragment。当然这种方法很直白,但是有一个问题,如果图片很多怎么办,那么我们就讨论下一个问题了。
Fragment与Activity的通信
要实现Fragment与Activity的通信,步骤如下:
1.Fragment获取它所在的Activity,调用getActivity()方法即返回当前Activity。
2.Activity获取它所包含的Fragment,调用Activity的FragmentManager的findFragment(int id)或者findFragmentByTag(String tag)方法即可。
然而,有可能Fragment要和Activity交换信息,所以有:
1.Activity向Fragment发送数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将数据传递给Fragment。
2.Fragment向Activity发送数据或Activity需要在Fragment内部实现时事通信,需要在Fragment中定义一个内部回调接口,再让Fragment的Activity实现该接口即可。
下面我们用更简洁的方法来实现:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MyFragment extends Fragment{
public static final String KEY="key";
int id;
int[] images=new int[]{
R.drawable.cat,R.drawable.tiger,R.drawable.wolf
};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(getArguments().containsKey(KEY)){
id=getArguments().getInt(KEY);
}
}
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.fragment_view, container, false);
((ImageView)rootView.findViewById(R.id.show)).setImageResource(images[id]);
return rootView;
}
}
这次我们只需要一个Fragment,通过Activity发来的信息改变Fragment上的组件。
这个是Fragment的布局界面:
<?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="vertical" >
<ImageView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>"

</LinearLayout>
下边是MainActivity的代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next=(Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle=new Bundle();
bundle.putInt(MyFragment.KEY, (i++)%3);
MyFragment fragment=new MyFragment();
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.showfragment, fragment).commit();
}
});
}
}
然后就是MainActivity的布局界面代码,比较简单:
<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.fragmentsendmsg.MainActivity" >
<FrameLayout
android:id="@+id/showfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/next"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NEXT"/>"

</RelativeLayout>
两段代码实现了完全相同的功能,明显第二种方法更加简单,以后我们可视情况选择。

时间: 2024-12-10 20:09:00

Fragment的使用简介的相关文章

Fragment的使用简介【Android】

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="

Android开发之Fragment详解

Android开发之Fragment学习 1.简介: Fragment是Android 3.0引入的新API. Fragment代表了 Activity的子模块,因此可以把Fragment理解成Activity片段.Fragment用于自己的生命周期,也可以接受它自己的输入事件. Fragment必须被"嵌入" Activity中使用,因此虽然Fragment也拥有自己的生命周期,但Fragment的生命周期会受它所在的Activity的生命周期的控制.例如,当Activity暂停时,

fragment简介

Fragment简介 Fragment 表示 Activity 中的行为或用户界面部分.您可以将多个Fragment组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个Fragment.您可以将Fragment视为Activity 的模块化组成部分,它具有自己的生命周期,能接收自己的输入事件,并且您可以在 Activity 运行时添加或删除Fragment(有点像您可以在不同 Activity 中重复使用的"子Activity"). Frag

FragmentActivity与Fragment两者交互方法简介(转)

FragmentActivity与Fragment两者交互方法简介 分类: Fragment 2014-07-07 18:17 88人阅读 评论(0) 收藏 举报 在Android4.0后很多时候我们会大量使用到Fragment,Fragment与Activity的交互应该来说是非常重要的,但目前很多实例方法都只介绍了Fragment与Activity交互的方法,没有Activity与Fragment交互的方法,下面我来把解决的思路记录如下. 1.首先我需要定义一个公共接口,用于将Fragmen

android Fragment相关概念简介

Fragment 详细介绍连接:http://blog.csdn.net/harvic880925/article/details/44927375 fragment是一种控制器对象,activity可委派它完成一些任务,通常这些任务就是管理用户界面,受管理的用户界面可以是一整屏或是整屏的一部分, 管理用户界面的fragment又称UI fragment 它也有自己的布局文件视图,fragment视图包含了用户可以交互的可视化的UI元素, activity视图含有可供fragment视图插入的位

Android高级编程笔记(五)Fragment简介

Fragment是在Android 3.0 (API level 11)中引入的Activity的子模块.初衷是为了适应大屏幕的平板电脑,我们只需要使用Fragment对UI组件进行分组.模块化管理,就能很方便在运行过程中动态更新Activity的界面.Fragment必须被嵌入Activity中使用个,虽然也拥有自己的生命周期,但其生命周期手它所在的Activity的生命周期的控制.只有当该Activity处于活动状态时,我们才可以通过方法独立的操作Fragment. 一.Fragment的几

Thymeleaf3.0简介

thymeleaf的初次使用(带参请求以及调用带参js方法) 之前对于前端框架接触较少,第一次接触thymeleaf,虽说看起来并不复杂但我还是花费了好一会儿才弄懂. 话不多少下面就简单说一下我在项目中的应用. 首先是java代码 controller层 将需要在前端展示的信息放入model中: @RequestMapping("getAll") public String getAll(Model model){ List<ScheduleJob> list = sche

Android Fragment 的使用,一些你不可不知的注意事项

Fragment,俗称碎片,自 Android 3.0 开始被引进并大量使用.然而就是这样耳熟能详的一个东西,在开发中我们还是会遇见各种各样的问题,层出不穷.所以,是时候总结一波了. Fragment 简介 作为 Activity 界面的一部分,Fragment 的存在必须依附于 Activity,并且与 Activity 一样,拥有自己的生命周期,同时处理用户的交互动作.同一个 Activity 可以有一个或多个 Fragment 作为界面内容,并且可以动态添加.删除 Fragment,灵活控

Vivante GPU简介

目录 1. IMX6Q中Vivante GPU简介    1 1.1    IMX6Q中GPU型号    1 1.2    Vivante GC2000硬件架构    1 2. Vivante GPU使用的图形API简介    2 2.1    OpenGL ES    2 2.2    EGL    2 2.3    Vivante GPU软件框架    3 IMX6Q中Vivante GPU简介 IMX6Q中GPU型号 在imx6q中有3个vivante的GPU,分别是GC2000.GC32