21 RadioGroup ListFragment

时间:2023-03-09 08:47:02
21 RadioGroup ListFragment
  • 结构

    21 RadioGroup ListFragment

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数据源
private List<Fragment> list = new ArrayList<Fragment>(); private RadioButton[] rbs; private String[] titles={"news","happy","dz","cj"}; private int currentIndex =0;//当前展示的Fragment @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); rgMain = (RadioGroup) findViewById(R.id.rg_main); initData();
initTab();
} //初始化标签
private void initTab(){
//改变标签内容
rbs = new RadioButton[rgMain.getChildCount()];
for(int i=0;i<rgMain.getChildCount();i++){
rbs[i] = (RadioButton) rgMain.getChildAt(i);
rbs[i].setText(titles[i]);
} //点击按钮进行替换
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){//当前按钮被点击
//开始替换
//replaceFragment(i);
switchFragment(i);
} }
}
}); }
//replace 缺点 影响性能
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(); //点击的Fragment(目标)
Fragment targetFragment = list.get(targetIndex);
//当前的Fragment
Fragment currentFragment = list.get(currentIndex); //点击的 按钮对象的Fragment 存在 show()展示出来 隐藏当前的Fragment
if(!targetFragment.isAdded()){
transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();
}else{
transaction.show(targetFragment).hide(currentFragment).commit();
} //当前展示的Fragment就是点击替换的Fragment
currentIndex = targetIndex; } //初始化数据
private void initData(){
for(int i=0;i<rgMain.getChildCount();i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
//程序运行 默认展示第一个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数据源
private List<Fragment> list = new ArrayList<Fragment>(); private RadioButton[] rbs; private String[] titles={"news","happy","dz","cj"}; private int currentIndex =0;//当前展示的Fragment @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); rgMain = (RadioGroup) findViewById(R.id.rg_main); initData();
initTab();
} //初始化标签
private void initTab(){
//改变标签内容
rbs = new RadioButton[rgMain.getChildCount()];
for(int i=0;i<rgMain.getChildCount();i++){
rbs[i] = (RadioButton) rgMain.getChildAt(i);
rbs[i].setText(titles[i]);
} //点击按钮进行替换
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){//当前按钮被点击
//开始替换
//replaceFragment(i);
switchFragment(i);
} }
}
}); }
//replace 缺点 影响性能
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(); //点击的Fragment(目标)
Fragment targetFragment = list.get(targetIndex);
//当前的Fragment
Fragment currentFragment = list.get(currentIndex); //点击的 按钮对象的Fragment 存在 show()展示出来 隐藏当前的Fragment
if(!targetFragment.isAdded()){
transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();
}else{
transaction.show(targetFragment).hide(currentFragment).commit();
} //当前展示的Fragment就是点击替换的Fragment
currentIndex = targetIndex; } //初始化数据
private void initData(){
for(int i=0;i<rgMain.getChildCount();i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
//程序运行 默认展示第一个Fragment
getSupportFragmentManager().
beginTransaction().
add(R.id.layout_content_id, list.get(0)).commit();
} }

selecte_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="财经" />
</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-8"?>
<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-8"?>
<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>