多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開源 > 綜合技術(shù) > 21 RadioGroup ListFragment

21 RadioGroup ListFragment

來源:程序員人生   發(fā)布時(shí)間:2016-12-15 09:41:15 閱讀次數(shù):2757次
  • 結(jié)構(gòu)
    這里寫圖片描述

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>
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产高清在线精品免费不卡 | 有码在线视频 | 国产精品视频自拍 | 亚洲精品国产成人99久久 | 欧洲免费无线码二区5 | 国产精品免费福利 | 久久久91精品国产一区二区三区 | 国内精品一区视频在线播放 | 国产18视频 | 欧美videos粗暴高清性 | 国产日韩精品视频一区二区三区 | 中文字幕日韩专区 | 国产精品第44页 | 欧美freesex10一|3 | 国产免费网站看v片元遮挡 国产免费午夜a无码v视频 | 国产美女主播一级成人毛片 | xxxxx性中国hd| 操操操综合网 | 日韩免费| 日本成人性视频 | 中文字幕乱码在线观看 | 国内免费自拍视频 | 国产大逼 | 国产精品免费视频一区 | 精品久久久久不卡无毒 | 一级毛片在线不卡直接观看 | 最近免费中文字幕大全高清片 | 久久天天 | 亚洲黄色在线视频 | 伊人网在线视频 | 视频一区精品 | 2022国产成人精品福利网站 | 久久一区二区三区免费播放 | 天天狠狠弄夜夜狠狠躁·太爽了 | 欧美伦理片在线播放 | 国产在线喷潮免费观看 | 手机看片久久高清国产日韩 | 国产色综合久久无码有码 | 亚洲码在线观看 | 欧美人与动人物姣配xxxx | 亚洲精品欧美精品中文字幕 |