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@Overrideprotected 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() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubfor(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);//當前的FragmentFragment currentFragment = list.get(currentIndex);//點擊的 按鈕對象的Fragment 存在 show()展示出來 隱藏當前的Fragmentif(!targetFragment.isAdded()){transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();}else{transaction.show(targetFragment).hide(currentFragment).commit();}//當前展示的Fragment就是點擊替換的FragmentcurrentIndex = targetIndex;}//初始化數據private void initData(){for(int i=0;i<rgMain.getChildCount();i++){MyFragment myFragment = MyFragment.getInstance(i+1);list.add(myFragment);}//程序運行 默認展示第一個FragmentgetSupportFragmentManager().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@Overrideprotected 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() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubfor(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);//當前的FragmentFragment currentFragment = list.get(currentIndex);//點擊的 按鈕對象的Fragment 存在 show()展示出來 隱藏當前的Fragmentif(!targetFragment.isAdded()){transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();}else{transaction.show(targetFragment).hide(currentFragment).commit();}//當前展示的Fragment就是點擊替換的FragmentcurrentIndex = targetIndex;}//初始化數據private void initData(){for(int i=0;i<rgMain.getChildCount();i++){MyFragment myFragment = MyFragment.getInstance(i+1);list.add(myFragment);}//程序運行 默認展示第一個FragmentgetSupportFragmentManager().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>轉載于:https://www.cnblogs.com/muyuge/p/6152205.html
總結
以上是生活随笔為你收集整理的21 RadioGroup ListFragment的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入浅出mysql pdf_深入浅出My
- 下一篇: SSH三大框架理解