日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 21 RadioGroup ListFragment

21 RadioGroup ListFragment

來源:程序員人生   發布時間:2016-12-15 09:41:15 閱讀次數:2764次
  • 結構
    這里寫圖片描述

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>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 久久国产精品-国产精品 | 国产一区二区三区久久久 | 国产91免费看 | 亚洲成人精品一区 | 国产理论一区二区三区 | 在线观看1区 | 亚洲第一av在线 | 亚洲日韩中文字幕一区 | aa级黄色毛片 | 九九视频在线 | 精品综合99久久久久久www | 欧美精品免费在线观看 | 久久久精品免费 | 亚洲淫片 | 国产在视频一区二区三区吞精 | 国产精品久久久一区二区三区 | 国产精品午夜在线 | 久久国产伊人 | 91丨九色丨国产在线 | av一级免费观看 | 91综合网| 成人97| 精品一区二区三区久久 | 欧美99| 五月婷婷激情视频 | 国产一区 | av在线免费观看网站 | 毛片com | 色片在线免费观看 | 欧美日韩精品免费观看视频 | 国产欧美久久一区二区三区 | 黄色一几片 | 国产精品免费网站 | 91精品国产欧美一区二区成人 | 国产在线v | 日韩精品成人在线观看 | 在线欧美国产 | 91看片淫黄大片91桃色 | 欧州一区二区 | 91香蕉视频导航 | 欧美综合在线观看 |