Fragment:Activity片段
a)Fragment的特点:
(1)Fragment作为Activity界面的一部分组成出现
(2)可以在一个Activity中同时出现多个Fragment,一个Fragment亦可在多个Activity中使用。
(3)在Activity运行过程中,可以添加、移除或者替换Fragment(add()、remove()、replace())
(4)Fragment可以响应自己的输入事件,并且有自己的生命周期,当然,它们的生命周期直接被其所属的宿主activity的生命周期影响。只有当Activity处于活动状态时,程序员可通过方法独立的操作Fragment.
例如:Android3.0引入Fragment的初衷是为了适应
大屏幕的平台电脑,Framgment简化了大屏幕UI的设计,对UI组件进行分组,模块化管理,可以更方便地运行过程中动态更新Activity的用户界面,新闻浏览界面,该界面需要屏幕左边显示新闻列表,并在屏幕右边显示新闻内容。此时就可以再Activity中显示两个并排的Fragment,左边的Fragment显示新闻列表,右边的Fragment显示新闻内容。如果需要在正常尺寸的手机屏幕上运行该应用,可以改为使用两个Activity,ActivityA包含FragmentA,ActivityB包含FragmentB.
生命周期图
b)使用:
1、建立Fragment:创建类继承Fragment或其子类
2、实现相关的回调函数
通常, 可以实现如下的生命周期方法:onCreate():当创建fragment时, 系统调用该方法.
在实现代码中,应当初始化想要在fragment中保持的必要组件, 当fragment被暂停或者停止
后可以恢复.
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,
3、将Fragment加入宿主Activity
可以使用一下两种方式实现:
(1)在布局文件中配置:
<fragment
android:name="Fragment的className"
android:id="@+id/frag"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
(2)通过Activity的FragmentManager实现步骤:
1)获取FragmentManager对象
2)开启一个事务:beginTransaction()
3)transaction.add() 往Activity中添加一个Fragment
transaction.remove() 从Activity中移除一个Fragment 。
transaction.replace()使用另一个Fragment替换当前的,实际上就是
仅仅是设为不可见,并不会销毁 transaction.show()显示之前隐藏的Fragment
5)提交事务commit();
4.Fragment与Activity通信
Fragment可以通过getActivity()获取其宿主Activty的实例
Activity可以通过FragmentManager的findFragmentById()获取Fragment实例。
只要将Fragment装载进去就是Activity的一部分,获取控件和之前的方式一致。
5.Fragment的生命周期
与Activity的生命周期很类似,分为:活动状态,暂停状态,停止状态,销毁状态
onAttach():当Activity与Fragment发生关联时调用
onCreate():创建Fragment时被回调
onCreateView():创建或者绘制该Fragment的View组件时回调该方法,
Fragment将会显示该方法返回的View组件.
onActivityCreated():当Fragment所在的Activity的onCreate方法返回值回调该方法。
onStart():启动Fragment时被回调
onResume():恢复Fragment时被调用,onStart方法后一定会回调该方法
onPause():暂停Fragment时被调用
onStop():停止Fragment时被调用
onDestoryView():当该Fragment被移除时回调,
与oncreateView相对应。
onDestory():销毁Fragment时被回调
onDetach():与onAttach相对应,当该Fragment与Activity关联被取消时回调,被替换完成时回调该方法,
onDestory方法后一定回调该方法。
6.为Activity创建事件回调函数
在一些情况下, 可能需要一个fragment与activity分享事件.
一个好的方法是在fragment中定义一个回调的interface,并要求宿主activity实现它.
并且在Fragment的onAttach()方法中获取该Activity的实例。
activity通过interface接收到该回调,必要时它可以和在
layout中的其他fragment分享信息.
1.静态注册
package com.xspacing.fragment1;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
/**
*
* @ClassName MainActivity.java
* @Description TODO 静态注册fragment
* @author smile
* @version v1.0
* @date 2016年9月30日
*/
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.xspacing.fragment1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTop extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView t = new TextView(getActivity());
t.setGravity(Gravity.CENTER);
t.setText("FragmentTop");
return t;
}
}
package com.xspacing.fragment1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentBottom extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView t = new TextView(getActivity());
t.setGravity(Gravity.CENTER);
t.setText("FragmentBottom");
return t;
}
}
<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"
tools:context="com.xspacing.fragment1.MainActivity" >
<fragment
android:id="@+id/main_fragment_top"
android:name="com.xspacing.fragment1.FragmentTop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<fragment
android:id="@+id/main_fragment_bottom"
android:name="com.xspacing.fragment1.FragmentBottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
2.动态注册
package com.xspacing.fragment2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
/**
* @ClassName MainActivity.java
* @Description TODO 动态添加fragment
* @author smile
* @version v1.0
* @date 2016年9月30日
*/
public class MainActivity extends FragmentActivity implements OnClickListener {
private ImageView mRecommendIv;
private ImageView mMyIv;
private ImageView mNavigetionIv;
private FragmentManager fm;
private FragmentTransaction bt;
private RecommendFragment mRecommendFragment;
private NavigationFragment mNavigationFragment;
private MyFragment mMyFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mRecommendIv = (ImageView) findViewById(R.id.main_iv_recommend);
mNavigetionIv = (ImageView) findViewById(R.id.main_iv_navigation);
mMyIv = (ImageView) findViewById(R.id.main_iv_my);
mRecommendIv.setOnClickListener(this);
mNavigetionIv.setOnClickListener(this);
mMyIv.setOnClickListener(this);
settCurrentFragment(0);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_iv_recommend:
settCurrentFragment(0);
break;
case R.id.main_iv_navigation:
settCurrentFragment(1);
break;
case R.id.main_iv_my:
settCurrentFragment(2);
break;
default:
break;
}
}
void settCurrentFragment(int index) {
// 每次选中之前先清除掉上次的选中状态
clearNavImageView();
fm = getSupportFragmentManager();
bt = fm.beginTransaction();
// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
hideFragment(bt);
switch (index) {
case 0:
mRecommendIv.setImageResource(R.drawable.recommend_press);
if (mRecommendFragment == null) {
mRecommendFragment = new RecommendFragment();
bt.add(R.id.main_frame_fragment, mRecommendFragment, "mRecommendFragment");
} else {
bt.show(mRecommendFragment);
}
break;
case 1:
mNavigetionIv.setImageResource(R.drawable.navigation_press);
if (mNavigationFragment == null) {
mNavigationFragment = new NavigationFragment();
bt.add(R.id.main_frame_fragment, mNavigationFragment, "mNavigationFragment");
} else {
bt.show(mNavigationFragment);
}
break;
case 2:
mMyIv.setImageResource(R.drawable.my_press);
if (mMyFragment == null) {
mMyFragment = new MyFragment();
bt.add(R.id.main_frame_fragment, mMyFragment, "mMyFragment");
} else {
bt.show(mMyFragment);
}
break;
default:
break;
}
bt.commit();
}
// 清除掉上次的选中状态
private void clearNavImageView() {
mRecommendIv.setImageResource(R.drawable.recommend_not_press);
mNavigetionIv.setImageResource(R.drawable.navigation_not_press);
mMyIv.setImageResource(R.drawable.my_not_press);
}
// 将所有的Fragment都置为隐藏状态
private void hideFragment(FragmentTransaction bt) {
if (mRecommendFragment != null) {
bt.hide(mRecommendFragment);
}
if (mNavigationFragment != null) {
bt.hide(mNavigationFragment);
}
if (mMyFragment != null) {
bt.hide(mMyFragment);
}
}
}
package com.xspacing.fragment2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView t = new TextView(getActivity());
t.setGravity(Gravity.CENTER);
t.setText("我的");
return t;
}
}
package com.xspacing.fragment2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class NavigationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView t = new TextView(getActivity());
t.setGravity(Gravity.CENTER);
t.setText("导航");
return t;
}
}
package com.xspacing.fragment2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecommendFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView t = new TextView(getActivity());
t.setGravity(Gravity.CENTER);
t.setText("推荐");
return t;
}
}
<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"
tools:context="com.xspacing.fragment2.MainActivity" >
<FrameLayout
android:id="@+id/main_frame_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#44ff00ff" >
</FrameLayout>
<LinearLayout
android:id="@+id/main_linear_nav"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/main_iv_recommend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/recommend_not_press" />
<ImageView
android:id="@+id/main_iv_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/navigation_not_press" />
<ImageView
android:id="@+id/main_iv_my"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/my_not_press" />
</LinearLayout>
</LinearLayout>