MainActivity.java
package com.qf.day21_radiogroupfragment_demo3;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private RadioGroup rgMain;
//Fragment數(shù)據(jù)源
private List<Fragment> list = new ArrayList<Fragment>();
private RadioButton[] rbs;
private String[] titles={"news","happy","dz","cj"};
private int currentIndex =0;//當(dāng)前展現(xiàn)的Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgMain = (RadioGroup) findViewById(R.id.rg_main);
initData();
initTab();
}
//初始化標(biāo)簽
private void initTab(){
//改變標(biāo)簽內(nèi)容
rbs = new RadioButton[rgMain.getChildCount()];
for(int i=0;i<rgMain.getChildCount();i++){
rbs[i] = (RadioButton) rgMain.getChildAt(i);
rbs[i].setText(titles[i]);
}
//點(diǎn)擊按鈕進(jìn)行替換
rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0;i<rgMain.getChildCount();i++){
if(rbs[i].getId() == checkedId){//當(dāng)前按鈕被點(diǎn)擊
//開始替換
//replaceFragment(i);
switchFragment(i);
}
}
}
});
}
//replace 缺點(diǎn) 影響性能
public void replaceFragment(int index){
MyFragment myFragment = MyFragment.getInstance(index+1);
getSupportFragmentManager().
beginTransaction().
replace(R.id.layout_content_id, list.get(index)).commit();
}
//替換 使用 show() 和hide() 方法 減少性能開消
public void switchFragment(int targetIndex){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//點(diǎn)擊的Fragment(目標(biāo))
Fragment targetFragment = list.get(targetIndex);
//當(dāng)前的Fragment
Fragment currentFragment = list.get(currentIndex);
//點(diǎn)擊的 按鈕對(duì)象的Fragment 存在 show()展現(xiàn)出來 隱藏當(dāng)前的Fragment
if(!targetFragment.isAdded()){
transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();
}else{
transaction.show(targetFragment).hide(currentFragment).commit();
}
//當(dāng)前展現(xiàn)的Fragment就是點(diǎn)擊替換的Fragment
currentIndex = targetIndex;
}
//初始化數(shù)據(jù)
private void initData(){
for(int i=0;i<rgMain.getChildCount();i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
//程序運(yùn)行 默許展現(xiàn)第1個(gè)Fragment
getSupportFragmentManager().
beginTransaction().
add(R.id.layout_content_id, list.get(0)).commit();
}
}
MyFragment.java
package com.qf.day21_radiogroupfragment_demo3;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private RadioGroup rgMain;
//Fragment數(shù)據(jù)源
private List<Fragment> list = new ArrayList<Fragment>();
private RadioButton[] rbs;
private String[] titles={"news","happy","dz","cj"};
private int currentIndex =0;//當(dāng)前展現(xiàn)的Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgMain = (RadioGroup) findViewById(R.id.rg_main);
initData();
initTab();
}
//初始化標(biāo)簽
private void initTab(){
//改變標(biāo)簽內(nèi)容
rbs = new RadioButton[rgMain.getChildCount()];
for(int i=0;i<rgMain.getChildCount();i++){
rbs[i] = (RadioButton) rgMain.getChildAt(i);
rbs[i].setText(titles[i]);
}
//點(diǎn)擊按鈕進(jìn)行替換
rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0;i<rgMain.getChildCount();i++){
if(rbs[i].getId() == checkedId){//當(dāng)前按鈕被點(diǎn)擊
//開始替換
//replaceFragment(i);
switchFragment(i);
}
}
}
});
}
//replace 缺點(diǎn) 影響性能
public void replaceFragment(int index){
MyFragment myFragment = MyFragment.getInstance(index+1);
getSupportFragmentManager().
beginTransaction().
replace(R.id.layout_content_id, list.get(index)).commit();
}
//替換 使用 show() 和hide() 方法 減少性能開消
public void switchFragment(int targetIndex){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//點(diǎn)擊的Fragment(目標(biāo))
Fragment targetFragment = list.get(targetIndex);
//當(dāng)前的Fragment
Fragment currentFragment = list.get(currentIndex);
//點(diǎn)擊的 按鈕對(duì)象的Fragment 存在 show()展現(xiàn)出來 隱藏當(dāng)前的Fragment
if(!targetFragment.isAdded()){
transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();
}else{
transaction.show(targetFragment).hide(currentFragment).commit();
}
//當(dāng)前展現(xiàn)的Fragment就是點(diǎn)擊替換的Fragment
currentIndex = targetIndex;
}
//初始化數(shù)據(jù)
private void initData(){
for(int i=0;i<rgMain.getChildCount();i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
//程序運(yùn)行 默許展現(xiàn)第1個(gè)Fragment
getSupportFragmentManager().
beginTransaction().
add(R.id.layout_content_id, list.get(0)).commit();
}
}
selecte_main.xml
<?xml version="1.0" encoding="utf⑻"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@android:drawable/ic_menu_add"></item>
<item android:state_checked="false" android:drawable="@android:drawable/ic_menu_call"></item>
</selector>
activity_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"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/rg_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:checked="true"
android:text="新聞" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:text="文娛" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:text="體育" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:text="財(cái)經(jīng)" />
</RadioGroup>
<FrameLayout
android:id="@+id/layout_content_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
></FrameLayout>
</LinearLayout>
fragment_layout.xml
<?xml version="1.0" encoding="utf⑻"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="AAA"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf⑻"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/title_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="name"
/>
<TextView
android:id="@+id/content_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="aaa"
android:layout_alignBottom="@id/iv_item"
/>
</RelativeLayout>