Android Activity中嵌套多个Fragment的使用

来源:互联网 发布:mac safari 扩展 编辑:程序博客网 时间:2024/05/22 03:08

           Fragment英文翻译为碎片,可以在一个Activity中可以嵌套多个Fragment,相当于多个页面。但是它们都在同一   个Activity中,fragment的生命周期和Activity类似,如果当前Activity没有注销(onDestory)其中的众多的fragment也不会被销毁。

1.可以在XML中编写,也可以在代码中编写,写一个类继承android.support.v4.app.Fragment,记住:是V4包中的Fragment.(考虑到版本的兼容性问题:fragment是Android3.0才引入的)2.重写父类Fragment的onCreate(),和onCreateView()方法,onCreateView方法会返回一个VIew,这个View将被填充到Activity的界面中。所以一般我们重点就是把onCreateView()覆写好。</span>3.在Acticvty中产生一个fragment的实例,调用fragmentManager 和 fragmentTransaction;实现fragment的添加,移除,替换(add,remove,replace),记住,当这些事务写完以后要Commit(),保证事务被提交,这样才会生效。

</pre><pre code_snippet_id="423037" snippet_file_name="blog_20140709_6_921150" name="code" class="java">
下面是一个DEMO:import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.support.v4.app.FragmentManager;/**   Fragment的生命周期: * 第一次启动该fragment:   onCreate --> onCreateView -->onStart --> onResume *  变得不处于前台的时候:onPause  -->onDestroyView *  重新回到前台时候:OnCreateView -->onStart --> onResume * @author yesong * */public class ContentFragment extends Fragment {private Button mStartButton;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("yesongyesong", "ContentFragment : onCreate ");}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.content_fragment_layout, null);mStartButton = (Button) view.findViewById(R.id.start_button);mStartButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {MainActivity activity = (MainActivity) getActivity();activity.enterContentFrament01(mStartButton.getText().toString());}});return view;}}

第二个fragment

package cn.mmb.fragment.product;import android.annotation.SuppressLint;import android.os.Bundle;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import cn.mmb.fragment.MainActivity;import cn.mmb.fragment.R;import cn.mmb.fragment.inter.ViewClickListener;@SuppressLint("ValidFragment")public class Content01_Fragment extends Fragment {private Button mStartButton;/* * private ViewClickListener viewClickListener; public * Content01_Fragment(ViewClickListener clickListener){ * this.viewClickListener = clickListener; } */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.content_01_fragment_layout, null);mStartButton = (Button) view.findViewById(R.id.content01_button);mStartButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {MainActivity activity = (MainActivity) getActivity();activity.enterContentFrament02(mStartButton.getText().toString());}});
<span style="font-family: Arial, Helvetica, sans-serif;">return view;</span>

}}

第三个fragment

package cn.mmb.fragment.product;import android.annotation.SuppressLint;import android.os.Bundle;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import cn.mmb.fragment.MainActivity;import cn.mmb.fragment.R;import cn.mmb.fragment.inter.ViewClickListener;@SuppressLint("ValidFragment")public class Content02_Fragment extends Fragment {private Button mStartButton;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.content_02_fragment_layout, null);mStartButton = (Button) view.findViewById(R.id.no_more_button);mStartButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {}});return view;}}
Activity使用我们已经写好的fragment

package cn.mmb.fragment;import cn.mmb.fragment.product.ButtomFragment;import cn.mmb.fragment.product.Content01_Fragment;import cn.mmb.fragment.product.Content02_Fragment;import cn.mmb.fragment.product.ContentFragment;import cn.mmb.fragment.product.TitleBarFragment;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.KeyEvent;import android.view.View;import android.widget.FrameLayout;import android.widget.LinearLayout;public class MainActivity extends FragmentActivity/* implements ViewClickListener */{private TitleBarFragment mTitleBarFragment;private ContentFragment mContentFragment;private Content01_Fragment mContent01Fragment;private Content02_Fragment mContent02Fragment;private ButtomFragment mButtomFragment;//private FrameLayout mContentLayout;private LinearLayout mTitleBarLayout;private LinearLayout mButtomLayout;private FragmentTransaction fragmentTransaction;private FragmentManager fragmentManager;/** *  to remember  current fragment, *  if mfragmentIndex == 0 that there is currently a  contentFragmemt */private int mFragmentIndex = 0; /** *  to remember  last time title bar message */private String mLastTitleMessage;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTitleBarLayout = (LinearLayout) findViewById(R.id.titlebar_layout);//mContentLayout = (FrameLayout) findViewById(R.id.content_layout);mButtomLayout = (LinearLayout) findViewById(R.id.buttom_layout);mTitleBarLayout.setVisibility(View.GONE);fragmentManager = getSupportFragmentManager();fragmentTransaction = fragmentManager.beginTransaction();mContentFragment = new ContentFragment();mButtomFragment = new ButtomFragment();     fragmentTransaction.add(R.id.content_layout, mContentFragment,null); fragmentTransaction.add(R.id.buttom_layout, mButtomFragment,null);/*fragmentTransaction.replace(R.id.content_layout, mContentFragment);fragmentTransaction.replace(R.id.buttom_layout, mButtomFragment);*/fragmentTransaction.commit();} /**   * Before using a callback to control other fragment and some view,  * but now find  a better method : Fragment.this.getActivity();  *//* @Override  public void buttonClick(View v, String msg) {    switch (v.getId()) {   case R.id.start_button: enterContentFrament01(msg);  break;  case R.id.content01_button: enterContentFrament02(msg); break;  default: break;   }   }*//** * enter the  Third content fragment * @param msg : the  titleBarMessage */public void enterContentFrament02(String msg) {if (mTitleBarFragment != null) {mLastTitleMessage = mTitleBarFragment.getTitleMessage();mTitleBarFragment.setTitleMessage(msg);}if (mContent02Fragment == null) {mContent02Fragment = new Content02_Fragment();}fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.content_layout, mContent02Fragment);//fragmentTransaction.add(R.id.content_layout, mContent02Fragment);fragmentTransaction.addToBackStack(null);fragmentTransaction.commit();mFragmentIndex++;}/** * enter the  second content fragment * @param msg : the  titleBarMessage */public void enterContentFrament01(String msg) {if (mTitleBarFragment == null) {mTitleBarFragment = new TitleBarFragment(msg);}mLastTitleMessage = mTitleBarFragment.getTitleMessage();mTitleBarFragment.setTitleMessage(msg);  //set TitleBar messageif (mContent01Fragment == null) {mContent01Fragment = new Content01_Fragment();}mTitleBarLayout.setVisibility(View.VISIBLE);   mButtomLayout.setVisibility(View.GONE);fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.titlebar_layout, mTitleBarFragment);fragmentTransaction.replace(R.id.content_layout, mContent01Fragment);/*fragmentTransaction.add(R.id.titlebar_layout, mTitleBarFragment);fragmentTransaction.add(R.id.content_layout, mContent01Fragment);*/fragmentTransaction.addToBackStack(null);fragmentTransaction.commit();mFragmentIndex++;}@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {mFragmentIndex--;if (mFragmentIndex == 0) { mButtomLayout.setVisibility(View.VISIBLE);mTitleBarLayout.setVisibility(View.GONE);}}if (mTitleBarFragment != null) {mTitleBarFragment.setTitleMessage(mLastTitleMessage);}return super.onKeyUp(keyCode, event);}}

运行效果图:
         

   第一个fragment  (白色)                   第2个fragment  (黄色)              第3个fragment(红色)

  点击button进入第2个fragment           点击button进入第3个fragment                    

  下面的的menu也是fragment              上面的titleBar也是一个fragment

 

以上是多个fragment都在同一个Activity中跳转。


       

0 0