日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

21 RadioGroup ListFragment

發(fā)布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 21 RadioGroup ListFragment 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 結(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;//當前展示的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(){//改變標簽內(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]);}//點擊按鈕進行替換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;}//初始化數(shù)據(jù)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數(shù)據(jù)源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(){//改變標簽內(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]);}//點擊按鈕進行替換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;}//初始化數(shù)據(jù)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="財經(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-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>

轉(zhuǎn)載于:https://www.cnblogs.com/muyuge/p/6152205.html

總結(jié)

以上是生活随笔為你收集整理的21 RadioGroup ListFragment的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。