android入門——用RadioGroup實現Fragment的切換

個人網站:極客時代

用RadioGroup設置功能menu,通過點擊不同的RadioButton切換不同的Fragment。

佈局文件:activity_main.xml(除此之外,就是幾個Fragment的佈局文件了)

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.Ray.testshow.MainActivity" 
    android:orientation="vertical">"


	<!-- 裝fragment的容器 -->
    <FrameLayout android:id="@+id/fragment_container"
        android:layout_height="0px"
        android:layout_width="match_parent"
        android:layout_weight="7">
        
    </FrameLayout>
    
    <RadioGroup android:id="@+id/RGMenu"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:orientation="horizontal">"
        <!-- 將RadioGroup放在最底部,裏面的按鈕居中顯示 -->
        
        <RadioButton style="@style/rb_style"
            android:drawableTop="@drawable/menu1_click"
            android:text="Menu1"/>
        
        <RadioButton style="@style/rb_style"
            android:drawableTop="@drawable/menu2_click"
            android:text="Menu2"/>
        
        <RadioButton style="@style/rb_style"
            android:drawableTop="@drawable/menu3_click"
            android:text="Menu3"/>
        
        <RadioButton style="@style/rb_style"
            android:drawableTop="@drawable/menu4_click"
            android:text="Menu4"/>
        
        <RadioButton style="@style/rb_style"
            android:drawableTop="@drawable/menu5_click"
            android:text="Menu5"/>
        
    </RadioGroup>

</LinearLayout>

menu1_click.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 設置RadioButton在未點擊、點擊及選中的時候的圖片顯示,以與其他的RadiosButton區分 -->
    <item android:drawable="@drawable/menu1" android:state_pressed="true"/>
    <item android:drawable="@drawable/menu1" android:state_checked="true"/>
    <item android:drawable="@drawable/menu1_be"/>
    <!-- 把默認值放在最後,否則會一直保持在默認狀態無法改變 -->
</selector>



Java文件:

MainActivity.java

package com.Ray.testshow;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//默認的fragment
		FragmentManager fManager = getFragmentManager();
		FragmentTransaction fTransaction = fManager.beginTransaction();
		Fragment fragment = FragmentCheck.getFragmentById(1);
		fTransaction.add(R.id.fragment_container, fragment);
		fTransaction.commit();
		
		//通過點擊不同的RadioButton進行切換
		RadioGroup rGroup = (RadioGroup)findViewById(R.id.RGMenu);
		rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				FragmentManager fManager = getFragmentManager();
				FragmentTransaction fTransaction = fManager.beginTransaction();
				Fragment fragment = FragmentCheck.getFragmentById(checkedId);
				
					fTransaction.replace(R.id.fragment_container, fragment);
					fTransaction.commit();
			}
		});
	}

}

FragmentCheck.java(通過傳入不同的值,來生成不同的fragment對象)

package com.Ray.testshow;

import android.app.Fragment;

public class FragmentCheck {
	public static Fragment getFragmentById(int checkedID) {
		Fragment fragment = null;
		switch (checkedID) {
		case 1:
			fragment=new FragmentMenu1();
			break;

		case 2:
			fragment=new FragmentMenu2();
			break;
			
		case 3:
			fragment=new FragmentMenu3();
			break;

		case 4:
			fragment=new FragmentMenu4();
			break;
		
		case 5:
			fragment=new FragmentMenu5();
			break;
		default:
			break;
		}
		return fragment;
	}
}

其他的java文件都是fragment對應的了。

寫了一個比較簡單的Demo,雖然不是很好看,但是能看出怎樣用的。換幾張RadioButton的圖片,效果會好很多,沒時間去自己P了,醜些。

示例代碼下載地址:

http://download.csdn.net/detail/darkxionghaizi/9078709


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章