Fragment切换页面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/a" android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Fragment A"/> <Button android:id="@+id/b" android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Fragment B"/> </LinearLayout> </LinearLayout>
FragmentActivity.java
package com.zyf; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.FrameLayout; /** * fragment 使用实例 * @see http://developer.android.com/training/basics/fragments/fragment-ui.html * * 3.0不需要继承FragmentActivity,因为3.0将比如getFragmentManager()方法已经加入到Activity中了。 * * 3.0以前版本要通过继承FragmentActivity获得类似功能。 */ public class FragmentActivity extends android.support.v4.app.FragmentActivity { FragmentManager fragmentManager; FrameLayout container; FragmentA a; FragmentB b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 得到框架布局控件 container = (FrameLayout)findViewById(R.id.container); // 返回与此活动相关的片段进行交互的FragmentManager fragmentManager = this.getSupportFragmentManager(); // 通过begintransaction方法获取一个事物处理实例。 FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction(); a = new FragmentA(); b = new FragmentB(); /** 在这期间可以使用 add(), remove(), 以及 replace(). 最终需要改变时执行 commit()即可 */ mFragmentTransaction.add(R.id.container, a); mFragmentTransaction.commit(); } public void click(View view) { switch (view.getId()) { case R.id.a: // 按钮A show(a); break; case R.id.b: // 按钮B show(b); break; default: break; } } private void show(Fragment frament) { FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction(); mFragmentTransaction.replace(R.id.container, frament); mFragmentTransaction.addToBackStack(null); // mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果 mFragmentTransaction.commit(); } }
FragmentA.java
package com.zyf; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class FragmentA extends Fragment { @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.fa, container, false); // "AAAAAAAAAAAAA"按钮 Button btn = (Button)view.findViewById(R.id.next); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW); startActivity(intent); } }); return view; } // public void next(View view) { // Intent intent = new Intent(Intent.ACTION_VIEW); // startActivity(intent); // } }
FragmentB.java
package com.zyf; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentB extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fb, container, false); } // public void next(View view) { // Intent intent = new Intent(Intent.ACTION_VIEW); // startActivity(intent); // } }