关键代码来自:http://www.cnblogs.com/tiantianbyconan/p/3360938.html
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class FixturesChildActivity extends Activity {
private RadioGroup mRGSwitch;// 单选按钮组
public List<Fragment> fragments = new ArrayList<Fragment>();
private ArrayList<View> viewLines = new ArrayList<View>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取RadioButton下高亮的黄线
View line1 = this.findViewById(R.id.fixtures_child_left_radio_button_line);
View line2 = this.findViewById(R.id.fixtures_child_chatroom_button_line);
View line3 = this.findViewById(R.id.fixtures_child_right_radio_button_line);
View line4 = this.findViewById(R.id.fixtures_child_caipiao_radio_button_line);
//加入集合
viewLines.add(line1);
viewLines.add(line2);
viewLines.add(line3);
viewLines.add(line4);
fragments.add(new TabAFm());
fragments.add(new TabBFm());
fragments.add(new TabCFm());
fragments.add(new TabDFm());
fragments.add(new TabEFm());
mRGSwitch = (RadioGroup) findViewById(R.id.fixtures_child_radio_group);
new FragmentsAdapter(FixturesChildActivity.this, fragments,
R.id.fixtures_child_view_child_view, mRGSwitch);
}
/**
* fragment适配器
*/
public class FragmentsAdapter implements RadioGroup.OnCheckedChangeListener {
private List<Fragment> fragments; // 一个tab页面对应一个Fragment
private RadioGroup rgs; // 用于切换tab
private Activity mActivity; // Fragment所属的Activity
private int fragmentContentId; // Activity中所要被替换的区域的id
private int currentTab; // 当前Tab页面索引
public FragmentsAdapter(Activity fragmentActivity,
List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
this.fragments = fragments;
this.rgs = rgs;
this.mActivity = fragmentActivity;
this.fragmentContentId = fragmentContentId;
// 默认显示第一页
FragmentTransaction ft = fragmentActivity.getFragmentManager()
.beginTransaction();
ft.add(fragmentContentId, fragments.get(0));
ft.commit();
rgs.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
for (int i = 0; i < rgs.getChildCount(); i++) {
if (rgs.getChildAt(i).getId() == checkedId) {
//把前一个RadioButton文本颜色设置为白色
((RadioButton) rgs.getChildAt(getCurrentTab()))
.setTextColor(getResources()
.getColor(R.color.white));
//把本次点击RadioButton文本颜色设置为蓝色
((RadioButton) rgs.getChildAt(i))
.setTextColor(getResources().getColor(R.color.blue));
viewLines.get(getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.transparent));
viewLines.get(i).setBackgroundColor(getResources().getColor(R.color.light_yellow));
Fragment fragment = fragments.get(i);
FragmentTransaction ft = mActivity.getFragmentManager()
.beginTransaction();
getCurrentFragment().onPause(); // 暂停当前tab
if (fragment.isAdded()) {
fragment.onResume(); // 启动目标tab的onResume()
} else {
ft.add(fragmentContentId, fragment);
}
showTab(i); // 显示目标tab
ft.commit();
}
}
}
/**
* 切换tab
*
* @param idx
*/
private void showTab(int idx) {
for (int i = 0; i < fragments.size(); i++) {
Fragment fragment = fragments.get(i);
FragmentTransaction ft = mActivity.getFragmentManager()
.beginTransaction();
if (idx == i) {
ft.show(fragment);
} else {
ft.hide(fragment);
}
ft.commit();
}
currentTab = idx; // 更新目标tab为当前tab
}
public int getCurrentTab() {
return currentTab;
}
public Fragment getCurrentFragment() {
return fragments.get(currentTab);
}
}
}
另一实现方式:http://www.cnblogs.com/over140/p/3362047.html
此外可以,将Fragment放置于ViewPager中,自动保存其状态。