您的位置:首页 > 其它

Fragment讲解

2015-08-21 22:55 253 查看
Android是在Android 3.0 (API level 11)开始引入Fragment的。

  可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。

  可以把Fragment设计成可以在多个Activity中复用的模块。

  当开发的应用程序同时适用于平板电脑和手机时,可以利用Fragment实现灵活的布局,改善用户体验。

Fragment的生命周期

  因为Fragment必须嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相关的。

  如果Activity是暂停状态,其中所有的Fragment都是暂停状态;如果Activity是stopped状态,这个Activity中所有的Fragment都不能 被启动;如果Activity被销毁,那么它其中的所有Fragment都会被销毁。

  但是,当Activity在活动状态,可以独立控制Fragment的状态,比如加上或者移除Fragment。

  当这样进行fragment transaction(转换)的时候,可以把fragment放入Activity的back stack中,这样用户就可以进行返回操作。

当创建包含Fragment的Activity时,如果用的是Support Library,那么继承的就应该是FragmentActivity而不Activity。

Fragment的常见使用方法

必须实现的三个回调函数

  onCreate()

  系统在创建Fragment的时候调用这个方法,这里应该初始化相关的组件,一些即便是被暂停或者被停止时依然需要保留的东西。

  onCreateView()

  当第一次绘制Fragment的UI时系统调用这个方法,必须返回一个View,如果Fragment不提供UI也可以返回null。

  注意,如果继承自ListFragment,onCreateView()默认的实现会返回一个ListView,所以不用自己实现。

  onPause()

  当用户离开Fragment时第一个调用这个方法,需要提交一些变化,因为用户很可能不再返回来。

实现Fragment的UI

  提供Fragment的UI,必须实现onCreateView()方法。

  假设Fragment的布局设置写在example_fragment.xml资源文件中,那么onCreateView()方法可以如下写:

public static class ExampleFragment extends Fragment

{

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

  Bundle savedInstanceState)

{

// Inflate the layout for this fragment

return inflater.inflate(R.layout.example_fragment, container, false);

}

}

  onCreateView()中container参数代表该Fragment在Activity中的父控件;savedInstanceState提供了上一个实例的数据。

  inflate()方法的三个参数:

  第一个是resource ID,指明了当前的Fragment对应的资源文件;

  第二个参数是父容器控件;

  第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。

把Fragment加入Activity

  当Fragment被加入Activity中时,它会处在对应的View Group中。

  Fragment有两种加载方式:一种是在Activity的layout中使用标签<fragment>声明;另一种方法是在代码中把它加入到一个指定的ViewGroup中。

  另外,Fragment它可以并不是Activity布局中的任何一部分,它可以是一个不可见的部分。这部分内容先略过。

加载方式1:通过Activity的布局文件将Fragment加入Activity

  在Activity的布局文件中,将Fragment作为一个子标签加入即可。

  如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>


  其中android:name属性填上你自己创建的fragment的完整类名。

  当系统创建这个Activity的布局文件时,系统会实例化每一个fragment,并且调用它们的onCreateView()方法,来获得相应fragment的布局,并将返回值插入fragment标签所在的地方。

  有三种方法为Fragment提供ID:

  android:id属性:唯一的id

  android:tag属性:唯一的字符串

  如果上面两个都没提供,系统使用容器view的ID。

加载方式2:通过编程的方式将Fragment加入到一个ViewGroup中

  当Activity处于Running状态下的时候,可以在Activity的布局中动态地加入Fragment,只需要指定加入这个Fragment的父View Group即可。

  首先,需要一个FragmentTransaction实例: 

FragmentManager fragmentManager = getFragmentManager()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

  (注,如果import android.support.v4.app.FragmentManager;那么使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)

  之后,用add()方法加上Fragment的对象:

ExampleFragment fragment = new ExampleFragment();

fragmentTransaction.add(R.id.fragment_container, fragment);

fragmentTransaction.commit();

  其中第一个参数是这个fragment的容器,即父控件组。

  最后需要调用commit()方法使得FragmentTransaction实例的改变生效。

下面用一个例子来演示:

主activity

package com.example.fragement;

import android.app.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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

private TextView tv1;
private TextView tv2;
private TextView tv3;

private Fragment tab1;
private Fragment tab2;
private Fragment tab3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

initView();
initEvent();
setSelect(0);
}

//设置Fragment内容区域
private void setSelect(int i) {
// TODO Auto-generated method stub
//将fragement通过动态添加方式添加到viewGroup中
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction trs = fm.beginTransaction();
//隐藏Fragment
hideFragement(trs);

//把text切换为选中
switch(i){
case 0:
if(tab1==null)
{
tab1=new Fragement1();
trs.add(R.id.content, tab1);
}
else {
trs.show(tab1);
}
tv1.setText("选中");
break;
case 1:
if(tab2==null)
{
tab2=new Fragement2();
trs.add(R.id.content,tab2);
}
else{
trs.show(tab2);
}
tv2.setText("选中");
break;
case 2:
if(tab3==null)
{
tab3=new Fragement3();
trs.add(R.id.content, tab3);
}
else{
trs.show(tab3);
}
tv3.setText("选中");
break;

default:
break;
}
//提交
trs.commit();
}

//隐藏所有的Fragment
private void hideFragement(FragmentTransaction trs){
if(tab1!=null)
{
trs.hide(tab1);
}
if(tab2!=null)
{
trs.hide(tab2);
}
if(tab3!=null)
{
trs.hide(tab3);
}
}

public void reset()
{
tv1.setText("海贼一号");
tv2.setText("海贼二号");
tv3.setText("海贼三号");
}
//初始化点击事件
private void initEvent() {
// TODO Auto-generated method stub
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
}

//初始化,创造实例
private void initView() {
// TODO Auto-generated method stub
tv1=(TextView) findViewById(R.id.textView1);
tv2=(TextView) findViewById(R.id.textView2);
tv3=(TextView) findViewById(R.id.textView3);
}
/**
* 每次点击之前都先做好准备工作,点击后会根据点击的地方返回id
* @param v
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
reset();
switch(v.getId()){
case R.id.textView1:
setSelect(0);
break;
case R.id.textView2:
setSelect(1);
break;
case R.id.textView3:
setSelect(2);
break;
default:
break;
}
}
}
主xml:

<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"
android:orientation="vertical"
>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/content"
>
</FrameLayout>
<include layout="@layout/bottom"/>
</LinearLayout>
bottom.xml:

<?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="45dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/first"
android:gravity="center"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="18sp"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/second"
android:gravity="center"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="18sp"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/third"
android:gravity="center"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="18sp"
/>
</LinearLayout>
tab1.xml

<?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"
android:background="@drawable/first"
>
</LinearLayout>
tab2.xml:

<?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"
android:background="@drawable/eight"
>

</LinearLayout>
tab3.xml:

<?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"
android:background="@drawable/five"
>
</LinearLayout>


效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: