和宿主activity的关系:
Fragment的生命周期和相关UI框架搭建
- onAttach(Activity) called once the fragment is associated with its activity.
- onCreate(Bundle) called to do initial creation of the fragment.
- onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
- onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
- onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
- onStart() makes the fragment visible to the user (based on its containing activity being started).
- onResume() makes the fragment interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, itgoes through a reverse series of callbacks:
- onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
- onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
- onDestroyView() allows the fragment to clean up resources associated with its View.
- onDestroy() called to do final cleanup of the fragment's state.
- onDetach() called immediately prior to the fragment no longer being associated with its activity.
viewpager和fragment的联合使用方式
①向ViewPager中添加fragment。
利用FragmenPagerAdapter:
一、需求:viewpager和fragment连用
二、思路:向viewpager中添加fragment必须用一个集合承载fragment对象,才能将viewpager和fragment绑定在一起实现联动。而且viewpager每次滑动时没必要每次都创建新的fragment对象,采用工厂模式生产fragment对象。
三、步骤:1、生产不同fragment对象
2、创建一个BaseFragment基类用于存储对象
3、自定义FragmentPagerAdapter子类
4、将viewpager和fragment绑定在一起
四、代码:
package cn.banwxf.googlepalay.ui.Fragment;
import java.util.HashMap;
import java.util.Map;
import android.support.v4.app.Fragment;
import cn.banwxf.googlepalay.ui.Fragment.impl.AppFragment;
importcn.banwxf.googlepalay.ui.Fragment.impl.CategoryFragment;
importcn.banwxf.googlepalay.ui.Fragment.impl.GameFragment;
importcn.banwxf.googlepalay.ui.Fragment.impl.HomeFragment;
import cn.banwxf.googlepalay.ui.Fragment.impl.HotFragment;
importcn.banwxf.googlepalay.ui.Fragment.impl.RecomendFragment;
importcn.banwxf.googlepalay.ui.Fragment.impl.TopicFragment;
/**
* 工厂模式创建fragment页面,因为页面是固定的。
* 用集合维护页面对象
*@author banwxf
*/
public class FactoryFragment {
privatestatic Map<Integer, BaseFragment>map = new HashMap<Integer, BaseFragment>();//因为页面数量固定所以用集合维护,以位置为标识
publicstatic Fragment createFragment(int pos) {
BaseFragmentfragment = map.get(pos);
if(fragment == null) {//只有当没有所需要的对象时才初始化对象
switch(pos) {
case0:
fragment= new HomeFragment();
break;
case1:
fragment= new AppFragment();
break;
case2:
fragment= new GameFragment();
break;
case3:
fragment= new TopicFragment();
break;
case4:
fragment= new RecomendFragment();
break;
case5:
fragment= new CategoryFragment();
break;
case6:
fragment= new HotFragment();
break;
}
//维护对象没必要每次都创建页面对象
map.put(pos,fragment);
}
returnfragment;
}
}
适配器:
classMyAdapter extends FragmentPagerAdapter {
publicMyAdapter(FragmentManager fm) {
super(fm);
}
//返回要显示的页面对象
@Override
publicFragment getItem(int position) {
returnFactoryFragment.createFragment(position);//创建指定位置的fragment
}
//数量
@Override
publicint getCount() {
returnmTabNames.length;//页面数量
}
}
②向fragment中添加viewpager等基本容器和布局
一、需求:fragment和其他布局和容器连用
二、思路:在fragment的onCreateView(LayoutInflater,ViewGroup, Bundle) 方法中实例化布局,因为有多个界面,需要一个基类,让子类分别去实现不同的布局。
三、步骤:1、写一个抽象的BaseFragment类继承Fragment,重写生命周期方法
2、方法回调
四:、代码:
package cn.banwxf.newsclient.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extendsFragment {
publicActivity mActivity;//这个就是mainActivity
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity= getActivity();// 获取当前fragment所依赖的activity
}
//fragment所依赖的activity的oncreate方法执行结束
@Override
publicvoid onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initDate();
}
//初始化fragment的布局
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
BundlesavedInstanceState) {
returninitView();
}
//必须由子类实现,初始化布局
publicabstract View initView();
//必须由子类实现,初始化数据
publicabstract void initDate();
}
子类实现两个抽象方法:View view = View.inflate(mActivity, R.layout.fragment_content,null);将view返回即可。