FragMent 的初相识(一)
【1】什么是Fragment ?
Fragment 是在Android 3.0 以后才出现的功能。
Android
运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板或者超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。
中文意思为“碎片”,与Activity 相似,就是其中多了几个方法。
可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现。
Fragment 用来在一个Activity 的方法中描述一些行为和一部分的用户的界面。
使用多个Fragment 可以在一个单独的Activity 中建立多个UI面板。
也可以在多个Activity 中重用Fragment
注意:
一个Fragment 必须被嵌入到一个Activity 中,其生命周期直接受其所属的宿主Activity 的生命周期影响。
例如:当Activity 被暂停时,其中的所有Fragment也被暂停;
当Activity 被销毁掉,其中的所属的Fragment 也被销毁;
当一个Activity 处于resumed(正在运行)状态时,可以单独地对每个Fragment 进行操作。
【2】Fragment 的两种创建方式
【2.1】Demo01 Fragment入门(静态的创建Fragment)
(1)创建一个类继承于 Fragment (
import
android.app.Fragment;
)
(2)重写onCreateView()方法,将Layout 对象转化为一个view 返回
(3)编写xml 文件,定义出来fragment.xml 文件,在ui 界面布局中,添加<fargment> 控件,指定实现Fragment类的name 的值。
(1)
Fragment_wx.java(无关是静态创建 还是 动态创建 都需要的这个方法)
package cn.edu.aynu.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_wx extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// [1]使用打气筒工具将布局文件转化为view ,并返回view
View v = inflater.inflate(R.layout.fragment_wx, container,false);
return v;
}
}
(2)activity_main.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="horizontal" >
<!-- name= 包名.类名 用于指定要添加的实例化的Fragment -->
<fragment
android:name="cn.edu.aynu.fragment.Fragment_wx"
android:id="@+id/fragment_wx"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment
android:name="cn.edu.aynu.fragment.Fragment_Contans"
android:id="@+id/fragment_contans"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</LinearLayout>
(3)fragment_wx.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是fragment_wx板块"
/>
</LinearLayout>
效果:
【2.2】Demo02 动态添加(查看横竖屏切换)
注意:多个Fragment 的操作的就像是 事务,要么一起成功,要么一起失败。
(1)创建类继承于 Fragment 重写其中的onCreateView() 方法。
(2)创建相关的fragment.xml 文件
(3)在MainActivity.java 中获取到Fragment 的管理者,开启事务,调用方法,提交事务
package cn.edu.aynu.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取手机屏幕的宽和高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth(); // 宽
int height = wm.getDefaultDisplay().getHeight(); // 高
// 【1】获取fargment 的管理者
FragmentManager fragmentManager = this.getFragmentManager();
// 【2】开启一个事务
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
if (width < height) {
// 竖屏
// 【3】
beginTransaction.replace(android.R.id.content, new Fragment_wx());
} else {
// 横屏
// 【3】
beginTransaction.replace(android.R.id.content, new Fragment_Contans());
}
// 【4】提交事务
beginTransaction.commit();
}
}
注意: 事务结束必须要进行提交
效果:
【2.3】仿微信的简单界面
package cn.edu.aynu.fangweixin;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
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;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private FragmentManager fragmentManager;
private Context context ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this ;
//【1】获取一个Fragment 的管理者
fragmentManager = this.getFragmentManager();
// 找到我们关心的控件
Button wx = (Button) findViewById(R.id.fragment_wx);
Button friend = (Button) findViewById(R.id.fragment_friend);
Button contans = (Button) findViewById(R.id.fragment_contans);
Button me = (Button) findViewById(R.id.fragment_me);
wx.setOnClickListener(this);
friend.setOnClickListener(this);
contans.setOnClickListener(this);
me.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// [1]开启事务
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.fragment_wx:
beginTransaction.replace(R.id.nihao, new Fragment_wx());
break;
case R.id.fragment_friend:
beginTransaction.replace(R.id.nihao, new Fragment_friend());
break;
case R.id.fragment_contans:
beginTransaction.replace(R.id.nihao, new Fragment_contans());
break;
case R.id.fragment_me:
beginTransaction.replace(R.id.nihao, new Fragment_me());
break;
default:
break;
}
// 关闭事务
beginTransaction.commit();
}
}
package cn.edu.aynu.fangweixin;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Fragment_wx extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
// [1]
View v = inflater.inflate(R.layout.fragment_wx, null);
Button button = (Button) v.findViewById(R.id.fragment_wx_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("nihao");
}
});
return v;
}
}
主布局
<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="cn.edu.aynu.fangweixin.MainActivity" >
<LinearLayout
android:id="@+id/nihao" <!-- 根据这个来找到布局的位置-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="4" >
<Button
android:id="@+id/fragment_wx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="微信" />
<Button
android:id="@+id/fragment_friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="朋友" />
<Button
android:id="@+id/fragment_contans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="联系人" />
<Button
android:id="@+id/fragment_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我" />
</LinearLayout>
</RelativeLayout>
效果