这篇blog主要是展示FragmentTransaction来管理Fragment,来达到Fragment的切换效果。
首先,来介绍一下google api关于FragmentTransaction的一些常用方法:
1,add (int containerViewId, Fragment fragment):
相信大家对这个方法比较熟悉,就是把Fragment添加到Activity中,这个Fragment(Fragment.onCreateView{returns non-null})可以把他的视图任意的插入Activity的容器视图
2,show(Fragment fragment):
显示先前隐藏的fragment,只适用那些视图已经被添加到容器中的Fragment,也就是说:在不确定的情况下,要先Fragment.isAdd()
3,hide(Fragment fragment):
隐藏已经存在的fragment,只适用那些视图已经被添加到容器中的Fragment,也就是说:在不确定的情况下,要先Fragment.isAdd()
4,addToBackStack(String name):
添加事务到backStack,也就是说当transaction.commit()调用之后,会记住这个transaction,随后当点击back键的时候,会从backStack中弹出,显示先前fragment状态。这样就能明白这个方法的使用时机了吧。(demo里我使用了这个方法,你可以试下back键的效果)
5,commit():普通提交
6,commitAllowingStateLoss():
效果很像commit,但是允许在activity状态保存之后提交。这是比较危险的,当activity重新restore的时候,commit有可能会丢失。因此使用时机是:当UI状态意外改变,对用户影响不大
7,replace(int containerViewId, Fragment fragment):
=remove(Fragment)+add(int,Fragment),即:先移除所有已经添加的Fragment,在添加目标fragment.
8,remove(Fragment fragment):
移除一个已经存在的fragment
先看下效果图:
下面开始贴代码(因为不难理解,所以我没有添加注释):
1,main.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"
android:background="#00ffff"
android:id="@+id/parent"
>
<FrameLayout
android:id="@+id/framelayout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="@+id/weixin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:drawableTop="@drawable/tab_weixin_normal"
android:gravity="center"
android:text="微信" />
<TextView
android:id="@+id/friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:drawableTop="@drawable/tab_find_frd_normal"
android:gravity="center"
android:text="找朋友" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:drawableTop="@drawable/tab_address_normal"
android:gravity="center"
android:text="地址" />
<TextView
android:id="@+id/setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:drawableTop="@drawable/tab_settings_normal"
android:gravity="center"
android:text="设置" />
LinearLayout>
LinearLayout>
2,MainActivity.java
package com.example.fragmentmanager2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener {
private FrameLayout frame;
private TextView weixin;
private TextView friend;
private TextView address;
private TextView setting;
private FragmentManager manager;
private AFragment a;
private BFragment b;
private CFragment c;
private DFragment d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getSupportFragmentManager();
initViews();
selectTab(0);
}
private void initViews() {
weixin = (TextView) findViewById(R.id.weixin);
weixin.setOnClickListener(this);
friend = (TextView) findViewById(R.id.friend);
friend.setOnClickListener(this);
address = (TextView) findViewById(R.id.address);
address.setOnClickListener(this);
setting = (TextView) findViewById(R.id.setting);
setting.setOnClickListener(this);
frame = (FrameLayout) findViewById(R.id.framelayout);
}
private void resetBtn() {
weixin.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_weixin_normal, 0, 0);
friend.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_find_frd_normal, 0, 0);
address.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_address_normal, 0, 0);
setting.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_settings_normal, 0, 0);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.weixin:
selectTab(0);
break;
case R.id.friend:
selectTab(1);
break;
case R.id.address:
selectTab(2);
break;
case R.id.setting:
selectTab(3);
break;
}
}
private void selectTab(int index) {
resetBtn();
FragmentTransaction transaction = manager.beginTransaction();
hideFragments(transaction);
switch (index) {
case 0:
weixin.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_weixin_pressed, 0, 0);
if (a == null) {
a = new AFragment();
transaction.add(R.id.framelayout, a);
} else {
Log.i("mess", "aaaa---"+a.isAdded());
if (a.isAdded()) {
transaction.show(a);
}
}
break;
case 1:
friend.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_find_frd_pressed, 0, 0);
if (b == null) {
b = new BFragment();
transaction.add(R.id.framelayout, b).addToBackStack(null);
} else {
Log.i("mess", "bbbb---"+b.isAdded());
if (b.isAdded()) {
transaction.show(b);
}
}
break;
case 2:
address.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_address_pressed, 0, 0);
if (c == null) {
c = new CFragment();
transaction.add(R.id.framelayout, c).addToBackStack(null);
} else {
Log.i("mess", "cccc---"+c.isAdded());
if (c.isAdded()) {
transaction.show(c);
}
}
break;
case 3:
setting.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.tab_settings_pressed, 0, 0);
if (d == null) {
d = new DFragment();
transaction.add(R.id.framelayout, d).addToBackStack(null);
} else {
Log.i("mess", "dddd---"+d.isAdded());
if (d.isAdded()) {
transaction.show(d);
}
}
break;
}
transaction.commitAllowingStateLoss();
}
private void hideFragments(FragmentTransaction transaction) {
if (a != null) {
transaction.hide(a);
}
if (b != null) {
transaction.hide(b);
}
if (c != null) {
transaction.hide(c);
}
if (d != null) {
transaction.hide(d);
}
}
}
3,a.xml(其他类似,只是text不同)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffff000"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这是a"
android:textSize="20sp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
LinearLayout>
4,AFragment.java(其他的类似,只是R.layout不同)
package com.example.fragmentmanager2;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* 项目名称:FragmentManager2
* 类名称:AFragment
* 类描述:
* 创建人:lc
* 创建时间:2015-2-11 下午4:18:05
* 修改人:131
* 修改时间:2015-2-11 下午4:18:05
* 修改备注:
*
* @version
*
*/
@SuppressLint("NewApi")
public class AFragment extends Fragment {
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (null == rootView) {
rootView = inflater.inflate(R.layout.a, container, false);
} else {
// 防止重复加载,出现闪退
if (null != rootView) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (null != parent) {
parent.removeView(rootView);
}
}
}
return rootView;
}
}
demo下载地址:http://download.csdn.net/detail/baidu_17508977/8477707
写的不好的地方,请大家多多指教,