好了废话不多说.看核心代码.下面有源码下载哦.
/**
* Project Name:com.zhuxiaohao.tab_05
* File Name:MainFragmentActivity.java
* Package Name:com.zhuxiaohao.tab_05
* Date:2015-4-12上午10:15:00
* Copyright (c) 2015, zhuxiaohao All Rights Reserved.
*
*/
package com.zhuxiaohao.tab_05;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
/**
* ClassName:MainFragmentActivity <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2015-4-12 上午10:15:00 <br/>
*
* @author chenhao
* @version
* @since JDK 1.6
* @see
*/
public class MainFragmentActivity extends FragmentActivity implements
OnClickListener {
private Context context;
Tab_1_fragment tab_1_fragment;
Tab_2_fragment tab_2_fragment;
Tab_3_fragment tab_3_fragment;
Tab_4_fragment tab_4_fragment;
TextView txt_tab_1;
TextView txt_tab_2;
TextView txt_tab_3;
TextView txt_tab_4;
FragmentManager fragmentManager;
private int number = 1;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.activity_main);
context = getApplicationContext();
fragmentManager = getSupportFragmentManager();
initView();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
outState.putBoolean("MainFragment", true);
super.onSaveInstanceState(outState);
}
/**
*
* initView:(初始化). <br/>
*
* @author chenhao
* @since JDK 1.6
*/
private void initView() {
// TODO Auto-generated method stub
txt_tab_1 = (TextView) findViewById(R.id.txt_tab_1);
txt_tab_2 = (TextView) findViewById(R.id.txt_tab_2);
txt_tab_3 = (TextView) findViewById(R.id.txt_tab_3);
txt_tab_4 = (TextView) findViewById(R.id.txt_tab_4);
txt_tab_1.setOnClickListener(this);
txt_tab_2.setOnClickListener(this);
txt_tab_3.setOnClickListener(this);
txt_tab_4.setOnClickListener(this);
setTabSelection(0);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.txt_tab_1:
setTabSelection(0);
break;
case R.id.txt_tab_2:
setTabSelection(1);
break;
case R.id.txt_tab_3:
setTabSelection(2);
break;
case R.id.txt_tab_4:
setTabSelection(3);
break;
}
}
/**
*
* setTabSelection:(根据传入的指数参数设置选中的标签页。). <br/>
*
* @author chenhao
* @param index
* @since JDK 1.6
*/
@SuppressLint({ "NewApi", "ResourceAsColor" })
public void setTabSelection(int index) {
resetBtn();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (index) {
case 0:
((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_1_fragment == null) {
tab_1_fragment = new Tab_1_fragment();
transaction.add(R.id.content_view, tab_1_fragment, index + "");
} else {
transaction.show(tab_1_fragment);
}
break;
case 1:
((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_2_fragment == null) {
tab_2_fragment = new Tab_2_fragment();
transaction.add(R.id.content_view, tab_2_fragment, index + "");
} else {
transaction.show(tab_2_fragment);
}
break;
case 2:
((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_3_fragment == null) {
tab_3_fragment = new Tab_3_fragment();
transaction.add(R.id.content_view, tab_3_fragment, index + "");
} else {
transaction.show(tab_3_fragment);
}
break;
case 3:
((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_4_fragment == null) {
tab_4_fragment = new Tab_4_fragment();
transaction.add(R.id.content_view, tab_4_fragment, index + "");
} else {
transaction.show(tab_4_fragment);
}
break;
}
transaction.commitAllowingStateLoss();
}
/**
*
* resetBtn:(重置). <br/>
*
* @author chenhao
* @since JDK 1.6
*/
@SuppressLint("ResourceAsColor")
private void resetBtn() {
((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
.setTextColor(getResources().getColorStateList(R.color.black));
((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
.setTextColor(getResources().getColorStateList(R.color.black));
((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
.setTextColor(getResources().getColorStateList(R.color.black));
((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
.setTextColor(getResources().getColorStateList(R.color.black));
}
/**
*
* hideFragments:(判断是否隐藏). <br/>
*
* @author chenhao
* @param transaction
* @since JDK 1.6
*/
@SuppressLint("NewApi")
private void hideFragments(FragmentTransaction transaction) {
if (tab_1_fragment != null) {
transaction.hide(tab_1_fragment);
}
if (tab_2_fragment != null) {
transaction.hide(tab_2_fragment);
}
if (tab_3_fragment != null) {
transaction.hide(tab_3_fragment);
}
if (tab_4_fragment != null) {
transaction.hide(tab_4_fragment);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (number < 1) {
finish();
return true;
} else {
number--;
Toast.makeText(context, "再按一次,退出应用", Toast.LENGTH_LONG).show();
return false;
}
} else {
return super.onKeyDown(keyCode, event);
}
}
}
下面说下关键部分代码.
/**
*
* setTabSelection:(根据传入的指数参数设置选中的标签页。). <br/>
*
* @author chenhao
* @param index
* @since JDK 1.6
*/
@SuppressLint({ "NewApi", "ResourceAsColor" })
public void setTabSelection(int index) {
resetBtn();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (index) {
case 0:
((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_1_fragment == null) {
tab_1_fragment = new Tab_1_fragment();
transaction.add(R.id.content_view, tab_1_fragment, index + "");
} else {
transaction.show(tab_1_fragment);
}
break;
case 1:
((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_2_fragment == null) {
tab_2_fragment = new Tab_2_fragment();
transaction.add(R.id.content_view, tab_2_fragment, index + "");
} else {
transaction.show(tab_2_fragment);
}
break;
case 2:
((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_3_fragment == null) {
tab_3_fragment = new Tab_3_fragment();
transaction.add(R.id.content_view, tab_3_fragment, index + "");
} else {
transaction.show(tab_3_fragment);
}
break;
case 3:
((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
.setTextColor(getResources()
.getColorStateList(R.color.blue));
if (tab_4_fragment == null) {
tab_4_fragment = new Tab_4_fragment();
transaction.add(R.id.content_view, tab_4_fragment, index + "");
} else {
transaction.show(tab_4_fragment);
}
break;
}
transaction.commitAllowingStateLoss();
}
通过 传入的参数到这里来判断当前 fragment 是否已经显示过了,如果显示过了,那么直接 show 出来,如果没有那么久 add进去.最后调用
transaction.commitAllowingStateLoss();
commit.这里直接一提的事.fragment 管理器是
fragmentManager = getSupportFragmentManager();
这个方法是重置所有 tab的选择状态.
/**
*
* resetBtn:(重置). <br/>
*
* @author chenhao
* @since JDK 1.6
*/
@SuppressLint("ResourceAsColor")
private void resetBtn() {
((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
.setTextColor(getResources().getColorStateList(R.color.black));
((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
.setTextColor(getResources().getColorStateList(R.color.black));
((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
.setTextColor(getResources().getColorStateList(R.color.black));
((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
.setTextColor(getResources().getColorStateList(R.color.black));
}
这个方法是判断是够隐藏
/**
*
* hideFragments:(判断是否隐藏). <br/>
*
* @author chenhao
* @param transaction
* @since JDK 1.6
*/
@SuppressLint("NewApi")
private void hideFragments(FragmentTransaction transaction) {
if (tab_1_fragment != null) {
transaction.hide(tab_1_fragment);
}
if (tab_2_fragment != null) {
transaction.hide(tab_2_fragment);
}
if (tab_3_fragment != null) {
transaction.hide(tab_3_fragment);
}
if (tab_4_fragment != null) {
transaction.hide(tab_4_fragment);
}
}
其他的东西太简单了,就不 贴出来了,下方有源码.