android 对开源项目FragmentOrientedApplication的理解

一,这是一个以Fragment为主导的app,主activity控制了全部子fragment的动作,项目结构如下:
1)Home  app里唯一的activity
2)HomeInterface  一推fragment和主activity的交互接口
3)BaseFragment  子fragment的基类
4)DrawerItemBaseFragment 拥有图片列表的另外一个子fragment的基类

二,HomeInterface的解析:
  这个总的接口是继承自其他5个子模块的接口。主要作用是:当自fragment要传信息到主ativity,或者fragment要让其他fragment干活的时候调用。
setSelectedFragment:作用是让子fragment在显示的时候保存自己的引用到主activity
popBackStack:作用是弹出堆栈顶端的fragment
popBackStackTillTag:  作用是立即弹出堆栈顶端的fragment
addFragment:作用是添加一个到 fragment 堆栈,并且显示出来,让主activity管理fragment。
addMultipleFragments: 作用是添加多个到 fragment 堆栈,并且显示出来,让主activity管理fragment。
其他的话:仅仅是普通的监听器,让fragment的信息传出来给其他fragment显示。

三,
1)每个具体的fragment里面定义一个唯一的TAG,如
private static final String TAG = "Greetings Fragment";

2)onCreate里保存着主actvity的接口引用,如
hostActivityInterface = (HostActivityInterface) getActivity();

3)onStart里保存主activity的fagment引用,如
hostActivityInterface.setSelectedFragment(this);

4)写几个方法,如
1,
public static GreetingsFragment instance(String name)  :
让instance的操作盒其他前提操作合并在一起,复用
2,
public String getTagText():
让主activity获得fragment的tag标识
3,
public boolean onBackPressed():
重新定义返回键在fragment里的使用,返回true自己处理返回键的事件,返回false让主activity处理
          然后可以看到主activity是这样写的:
public void onBackPressed() {
     // Check if selectedFragment is not consuming back press
     if(!selectedFragment.onBackPressed()) {
          // If not consumed, handle it.
          handleBackPressInThisActivity();
     }
}

4,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState):
这个是返回创建的一个view,普通的话里面通常只写 
         
 inflater.inflate一个view
,然后返回。 如果需要建立持久的view的话,应该这样写:
public View createPersistentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState, int      layout) {
     if (_rootView == null) {
          // Inflate the layout for this fragment
          _rootView = inflater.inflate(layout, null);
     } else {
          // Do not inflate the layout again.
          // The returned View of onCreateView will be added into the fragment.
          // However it is not allowed to be added twice even if the parent is same.
          // So we must remove _rootView from the existing parent view group
          // (it will be added back).
          ((ViewGroup)_rootView.getParent()).removeView(_rootView);
     }
    
     return _rootView;
}

原理是通过保存一个全局的view变量,当这个view不为空的时候,在onCreateView里直接返回,而原来parent view已经有这个view了,因此要removeView调,再返回。

5,
public void onViewCreated(View view, Bundle savedInstanceState)
执行完onCreateView后执行的,一般用来写findViewById等的后续相关代码。

5)传数据有几种方式:
一,调用方:
listDetailFragment.setArguments(arguments);
fragment在onViewCreated里拿,如
getArguments().getString();

二,调用方:
((Home) getActivity()).addFragment();
前提是主activity有相应的方法可以调用。

四,最后涉及到的fragment的FragmentTransaction,replace,commit,addToBackStack等名称就不一一介绍了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值