本文共 10659 字,大约阅读时间需要 35 分钟。
Fragment英文翻译为碎片,可以在一个Activity中可以嵌套多个Fragment,相当于多个页面。但是它们都在同一 个Activity中,fragment的生命周期和Activity类似,如果当前Activity没有注销(onDestory)其中的众多的fragment也不会被销毁,这点不利于资源的回收。所以有时候你要考虑是使用多个fragment还是使用多个Activity。
[java] view plaincopy在CODE上查看代码片派生到我的代码片
1.可以在XML中编写,也可以在代码中编写,写一个类继承android.support.v4.app.Fragment,记住:是V4包中的Fragment.(考虑到版本的兼容性问题:fragment是Android3.0才引入的)
2.重写父类Fragment的onCreate(),和onCreateView()方法,onCreateView方法会返回一个VIew,这个View将被填充到Activity的界面中。所以一般我们重点就是把onCreateView()覆写好。
3.在Acticvty中产生一个fragment的实例,调用fragmentManager 和 fragmentTransaction;实现fragment的添加,移除,替换(add,remove,replace),记住,当这些事务写完以后要Commit(),保证事务被提交,这样才会生效。
[java] view plaincopy在CODE上查看代码片派生到我的代码片
[java] view plaincopy在CODE上查看代码片派生到我的代码片下面是一个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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i("yesongyesong", "ContentFragment : onCreate "); } @Override public 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() { @Override public void onClick(View v) { MainActivity activity = (MainActivity) getActivity(); activity.enterContentFrament01(mStartButton.getText().toString()); } }); return view; } } 第二个fragment[java] view plaincopy在CODE上查看代码片派生到我的代码片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; } */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } } @Override public 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() { @Override public void onClick(View v) { MainActivity activity = (MainActivity) getActivity(); activity.enterContentFrament02(mStartButton.getText().toString()); } }); [java] view plaincopy在CODE上查看代码片派生到我的代码片 return view; [java] view plaincopy在CODE上查看代码片派生到我的代码片} 第三个fragment[java] view plaincopy在CODE上查看代码片派生到我的代码片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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public 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() { @Override public void onClick(View v) { } }); return view; } } Activity使用我们已经写好的fragment[java] view plaincopy在CODE上查看代码片派生到我的代码片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; @Override protected 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 message if (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++; } @Override public 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中跳转。
转载地址:http://gggwn.baihongyu.com/