国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 21 RadioGroup ListFragment

21 RadioGroup ListFragment

来源:程序员人生   发布时间:2016-12-15 09:41:15 阅读次数:2577次
  • 结构
    这里写图片描述

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); } //程序运行 默许展现第1个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); } //程序运行 默许展现第1个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="财经" /> </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>
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生