为什么要使用Fragment就不再多说了,网上有很多介绍,Fragment相对于Activity更灵活。为什么呢?Fragment与Activity有什么联系与区别呢?如果说Activity是一个大箱子的话,Fragment就是大箱子里面分装东西的小盒子,这样很容易理解东西分装在小盒子里更容易替换或更改,界面改变与设计也就更加灵活。这里就重点谈一下Fragment的入门使用动态加载与静态加载。
静态加载
特点:Fragment直接嵌入到Activity的xml文件中,这种方式使用fragment代码量少,但是灵活度低。
如下:直接在Activity的xml文件中嵌入MyFragment,然后对MyFragment进行设计,这里也可以嵌入多个,以一个为例。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/MyFragment"
android:name="com.example.learningtest1_fragment.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
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" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
细心地同学可能发现了一个问题,这两个xml文件,Actvity的xml文件中嵌入fragment,fragment的xml文件控制界面控件排版,那么二者的联系点在哪?也就是说activtiy怎么知道加载哪个fragment呢?注意activity中这一行 android:name="com.example.learningtest1_fragment.MyFragment"。没错就是根据类名来唯一标识这种联系的。很明显,我们还需要一个MyFragment的类来作为纽带。
package com.example.learningtest1_fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* resource:需要加载的布局文件
* root:加载layout的父viewgroup的
* false:不返回父viewgroup
*/
View view= inflater.inflate(R.layout.myfragment, container, false);
TextView tv=(TextView)view.findViewById(R.id.text1);
tv.setText("静态加载fragment");
return view;
}
}
如此,在Activity显示的时候,fragment中的视图就会显示在界面上。
动态加载
特点:不在Activity的xml中嵌入,而是在代码中使用FragmentManger控制加载。这种方式灵活度高,可以通过代码替换、增加、移除fragment,但是代价就是大量代码用于加载控制。
同样,创建一个类MyFragmnet2
package com.example.learningtest1_fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MyFragment2 extends android.app.Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* resource:需要加载的布局文件
* root:加载layout的父viewgroup的
* false:不返回父viewgroup
*/
View view= inflater.inflate(R.layout.myfragment2, container, false);
TextView tv=(TextView)view.findViewById(R.id.text1);
tv.setText("动态加载fragment");
return view;
}
}
其对应的Fragmnet2.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" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态加载按钮"/>
</LinearLayout>
在代码中控制fragment的加载
FragmentManager fm=getFragmentManager();
android.app.Fragment frame2=new MyFragment2();
android.app.FragmentTransaction ssss=fm.beginTransaction();
ssss.add(R.id.myframe, frame2);
ssss.commit();
需要注意的是我们要为主Activity的布局加上id,以便于在加载fragment时找到相应的位置
<LinearLayout
android:id="@+id/myframe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcdrffff"/>
</LinearLayout>
而在动态加载时就用到了id myframe:ssss.add(R.id.myframe, frame2);