Fragment的生命周期和相关UI框架搭建
来源:互联网 发布:mac光盘怎么弹出 编辑:程序博客网 时间:2024/04/30 14:10
和宿主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返回即可。
- Fragment的生命周期和相关UI框架搭建
- Fragment的生命周期及相关介绍
- Activity的生命周期和Fragment生命周期
- Fragment和Activity的生命周期
- Fragment和Activity的生命周期
- Activity和Fragment的生命周期
- Activity和Fragment的生命周期
- activity和fragment的生命周期
- Activity和Fragment的生命周期
- Activity 和 Fragment的生命周期
- activity和fragment的生命周期
- activity和fragment的生命周期
- Activity和Fragment的生命周期
- 软件框架的搭建【Fragment】
- Fragment生命周期相关
- 利用TabLayout和fragment搭建app框架
- Fragment和Activity的生命周期的关系
- Fragment的生命周期以及相关库函数的执行流程总结
- Android项目目录结构
- 解析 FBX 模型文件作为 Direct3D 的渲染模型
- 每天一个常用的linux命令(9)--cp
- idl之结构体
- 机器学习知识体系结构
- Fragment的生命周期和相关UI框架搭建
- go testing
- java代码实现对文件的base64加密解密
- 技术上遭遇的未解之“迷”们
- 云栖大会马总演讲:《未来属于善于拥抱未来的人》
- HDU 4556 法里数列
- 设计模式 - 享元模式
- iOS 金额余额从0跳动显示的动画
- CallableStatement 调用存贮过程