继承 Activity 或者FragmentActivity获得Manager的方式也不同
Activity :getFragmentManager()
FragmentActivity: getSupportFragmentManager()
或者说
导入的包名为
import android.app.FragmentTransaction;
或者
import android.support.v4.app.FragmentTransaction;
获取对象方式不一样
注:如果使用Android3.0以下的版本,需要引入v4的包
一:通过代码启动
包名:import android.app.FragmentTransaction;
1 、定义一个frameLayout布局
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
2、java
FragmentTransaction transaction = getFragmentManager().beginTransaction();
firstFragment = new FirstFragment();
transaction.add(R.id.fl_fragment, firstFragment);
transaction.commit();
每个transaction 只能commit()一次
实例:activity_first.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_firstFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="first Fragment" />
<Button
android:id="@+id/btn_secondFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="second Fragment" />
</LinearLayout>
<FrameLayout
android:id="@+id/fl_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
FirstActivity
import com.example.fragment_demo.R;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class FirstActivity extends Activity implements OnClickListener {
/**
* 用于展示第一个Fragment
*/
FirstFragment firstFragment;
/**
* 用于展示第二个Fragment
*/
SecondFragment secondFragment;
//两个标志
private final int FIRST_FRAGMENT_TRANSACTION = 1001;
private final int SECOND_FRAGMENT_TRANSACTION = 1002;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
StartFragment(FIRST_FRAGMENT_TRANSACTION);
findViewById(R.id.btn_firstFragment).setOnClickListener(this);
findViewById(R.id.btn_secondFragment).setOnClickListener(this);
}
private void StartFragment(int index) {
// 开启一个Fragment事务
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
hideFragments(transaction);
switch (index) {
case FIRST_FRAGMENT_TRANSACTION:
if (firstFragment == null) {
firstFragment = new FirstFragment();
transaction.add(R.id.fl_fragment, firstFragment);
} else {
transaction.show(firstFragment);
}
break;
case SECOND_FRAGMENT_TRANSACTION:
if (secondFragment == null) {
secondFragment = new SecondFragment();
transaction.add(R.id.fl_fragment, secondFragment);
} else {
transaction.show(secondFragment);
}
break;
default:
break;
}
transaction.commit();
}
/**
* 将所有的Fragment都置为隐藏状态。
*
* @param transaction
* 用于对Fragment执行操作的事务
*/
private void hideFragments(FragmentTransaction transaction) {
if (secondFragment != null) {
transaction.hide(secondFragment);
}
if (firstFragment != null) {
transaction.hide(firstFragment);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_firstFragment:
StartFragment(FIRST_FRAGMENT_TRANSACTION);
break;
case R.id.btn_secondFragment:
StartFragment(SECOND_FRAGMENT_TRANSACTION);
break;
default:
break;
}
}
}
二:通过布局中启动
<fragment android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fragment_content"
android:name="com.example.weather.fragweather"
/>
三: v4的fragment启动
如导的包为
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
private FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fl_fragment, mHomeFragment);
//transaction.show(mHomeFragment);
transaction.commit();