目录
4.基本程序单元Activity(三)
4.使用Fragment
Fragment是Android 3.0 新增的概念,其中文意思是“碎片”,它与Activity十分相似,用来在一个Activity中描述一些行为或一部分用户界面。使用多个Fragment可以在一个单独的Activity中建立多个UI面板,也可以在多个Activity中重用Fragment。例如,微信主界面就相当于一个Activity,在这个Activity 中包含多个Fragment,其中“微信”、“通讯录”、“发现”和“我”这4个功能界面,每一个功能界面就相当于一个Fragment,它们之间可以随意切换。
和Activity一样,Fragment 也有自己的生命周期。一个Fragment 必须被嵌入到一个Activity中,它的生命周期直接受其所属的宿主Activity的生命周期影响。例如,当Activity被暂停时,其中的所有Fragment也被暂停:当Activity被销毁时,所有隶属于它的Fragment也将被销毁。然而,当个Activity正在运行时(处于resumed状态),我们可以单独地对每一个 Fragment进行操作,如添加或删除等。
4-1.创建Fragment
要创建一个Fragment,必须创建一个Fragment的子类,或者继承另一个已经存在的Fragment的子类。例如,要创建一个名称为“NewsFragment 的Fragment",并重写onCreateView()方法,可以使用下面的代码:
public class NewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//从布局文件news.xml加载一个布局文件
View v = infater.inflate(R.layout.news,container, false);
return v;
}
}
注:当系统首次调用Fragment时,如果想绘制一个UI界面,那么在Fragment中必须重写onCreateView()方法返回一个View;如果Fragment没有UI界面,可以返回null。
4-2.在Activity中添加Fragment
向Activity中添加Fragment有两种方法:一种是直接在布局文件中添加,将Fragment作为Activity整个布局的一部分;另一种是当Activity运行时,将Fragment放入Activity 布局中。
1.直接在布局文件中添加Fragment
直接在布局文件中添加Fragment可以使用<fragment></fragment>标记实现。例如,要在一个布局文件中添加两个Fragment,可以使用下面的代码
<LinearLayout xmlns: android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation= "horizontal" >
<fragment android:name="com.mingrisoft.listFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.mingrisoft.DetailFragment"
android:id="@+id/detail"
android:layout_weight="2"
android:layout marginLeft="20dp"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
注:在<fragment>和</fragment>标记中,android:name 属性用于指定要添加的Fragment
2.当Activity运行时添加Fragment
当Activity运行时,也可以将Fragment添加到Activity 的布局中,实现方法是:首先获取一个FragmentTransaction的实例;然后使用add()方法添加一个Fragment, 其中add()方法的第一个参数是Fragment要放入的ViewGroup (由Resource ID指定),第二个参数是需要添加的Fragment;最后为了使改变生效,还必须调用commit()方法提交事务。例如,要在Activity运行时添加一个名称为“DetailFragment”的Fragment,可以使用下面的代码:
DetailFragment details = new DetailFragment();//实例化DetailFragment的对象
FragmentTransaction ft = getFragmentManager()
.beginTransaction();//获得一个FragmentTransaction的实例
ft.add(android.R.id.content, details);//添加一个显示详细内容的Fragment
ft.commit();
Fragment比较强大的功能之一就是可以合并两个Activity,从而让这两个Activity 在一个屏幕 上显示出来。
例:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<!-- Fragment组件-->
<fragment
android:id="@+id/main_fragment"
android:name="com.example.wechatinterfacechange.WeChat_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<!--微信图标-->
<ImageView
android:id="@+id/main_img1"
android:src="@drawable/bottom_1"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"/>
<!--通讯录图标-->
<ImageView
android:id="@+id/main_img2"
android:src="@drawable/bottom_2"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"/>
<!--发现图标-->
<ImageView
android:id="@+id/main_img3"
android:src="@drawable/bottom_3"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"/>
<!--我图标-->
<ImageView
android:id="@+id/main_img4"
android:src="@drawable/bottom_4"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.wechatinterfacechange;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//隐藏标题栏
ActionBar actionBar=getSupportActionBar();
actionBar.hide();
//获取布局文件的第一张导航图片
ImageView imageView1=(ImageView) findViewById(R.id.main_img1);
//获取布局文件的第二张导航图片
ImageView imageView2=(ImageView) findViewById(R.id.main_img2);
//获取布局文件的第三张导航图片
ImageView imageView3=(ImageView) findViewById(R.id.main_img3);
//获取布局文件的第四张导航图片
ImageView imageView4=(ImageView) findViewById(R.id.main_img4);
imageView1.setOnClickListener(l);//为第一张导航图片添加单击事件监听器
imageView2.setOnClickListener(l);//为第二张导航图片添加单击事件监听器
imageView3.setOnClickListener(l);//为第三张导航图片添加单击事件监听器
imageView4.setOnClickListener(l);//为第四张导航图片添加单击事件监听器
}
View.OnClickListener l=new View.OnClickListener(){
@Override
public void onClick(View view) {
//获取Fragment
FragmentManager fragmentManager=getSupportFragmentManager();
//开启一个事务
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//为Fragment初始化
Fragment fragment = null;
//通过获取单击的id判断单击了哪张图片
switch (view.getId()){
case R.id.main_img1:
//创建第一个Fragment
fragment=new WeChat_Fragment();
break;
case R.id.main_img2:
//创建第一个Fragment
fragment=new Message_Fragment();
break;
case R.id.main_img3:
//创建第一个Fragment
fragment=new Find_Fragment();
break;
case R.id.main_img4:
//创建第一个Fragment
fragment=new Me_Fragment();
break;
default:
break;
}
//替换Fragment
fragmentTransaction.replace(R.id.main_fragment,fragment);
//提交事务
fragmentTransaction.commit();
}
};
}
wechat_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/wf_img1"
android:src="@drawable/weixin"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
5.难点解答
5-1.Activity 和Fragment的区别
Activity和Fragment从展示形式上看,都是显示在屏幕上的。它们之间的区别主要体现在以下几个方面:
(1) Activity代表了一个屏幕的主体,而Fragment则可以作为Activity的一个组成元素,使用Fragment可以灵活地替换界面中的一部分。
(2)一个屏幕上只能有一个Activity, 一个Activiy中可以包含多个Fragment, Fragment需要被嵌入到Activity中。
(3) Fragment 的生命周期受其所属Activity影响,另外,Fragment 也有自己的生命周期,从而接收自己的输入事件。
(4)使用Fragmant切换时,要比多个Activity 切换平滑很多,即使在处理大数据时也很少出现卡顿的现象。