[Android学习笔记]fragment配合RadioGroup实现点击切换内容
这个主要是如何替换fragment的demo。效果图如下(下面的tabhost和上面的bar不属于这次的内容,这个是我做的一个应用程序框架的一部分,有需要的或者想研究研究的可以私下联系),主要是讲解中间的内容怎么实现,即点击上面的RadioGroup,下面的内容一起改变(改变的是XML中的布局,这样的话下面三个的布局完全可以自己定义)
1.首先在主界面的xml中添加一个RadioGroup,里面添加三个RadioButton即可
/** * 主Activity * * @author Gracker Gao * @date 2012.8.15 */package com.gracker.hostactivity;import android.app.Activity;import android.app.FragmentTransaction;import android.os.Bundle;import android.util.Log;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import com.gracker.fragment.SpeeddialFragmentOne;import com.gracker.fragment.SpeeddialFragmentThree;import com.gracker.fragment.SpeeddialFragmentTwo;import com.gracker.tabactivity.R;public class MainActivity extends Activity {private final String TAG = "SpeedDialActivity";private RadioGroup mRadioGroup;private SpeeddialFragmentTwo mSpeeddialFragmentTwo;private SpeeddialFragmentOne mSpeeddialFragmentOne;private SpeeddialFragmentThree mSpeeddialFragmentThree;private FragmentTransaction transaction;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.speeddial);init_date();setupWidgets();}private void init_date(){transaction = getFragmentManager().beginTransaction();if (null == mSpeeddialFragmentOne) {mSpeeddialFragmentOne = new SpeeddialFragmentOne();}transaction.add(R.id.fragment_container,mSpeeddialFragmentOne);// Commit the transactiontransaction.commit();}private void setupWidgets() {mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (checkedId) {case R.id.radio0:Log.v(TAG, "setupWidgets():radio0 clicked");if (null == mSpeeddialFragmentOne) {mSpeeddialFragmentOne = new SpeeddialFragmentOne();}transaction = getFragmentManager().beginTransaction();transaction.replace(R.id.fragment_container,mSpeeddialFragmentOne);// Commit the transactiontransaction.commit();break;case R.id.radio1:Log.v(TAG, "setupWidgets():radio1 clicked");if (null == mSpeeddialFragmentTwo) {mSpeeddialFragmentTwo = new SpeeddialFragmentTwo();}transaction = getFragmentManager().beginTransaction();transaction.replace(R.id.fragment_container,mSpeeddialFragmentTwo);// Commit the transactiontransaction.commit();break;case R.id.radio2:Log.v(TAG, "setupWidgets():radio2 clicked");if (null == mSpeeddialFragmentThree) {mSpeeddialFragmentThree = new SpeeddialFragmentThree();}transaction = getFragmentManager().beginTransaction();transaction.replace(R.id.fragment_container,mSpeeddialFragmentThree);// Commit the transactiontransaction.commit();break;default:break;}}});}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();// dataEncapsulation.closeDataBase_speedDial();}}
在RadioGroup的onCheckedChangeLinsteer中,切换Fragment。关于Fragment的一些操作,比如增加,删除,替换等等,可以参照这个帖子:http://www.eoeandroid.com/thread-71642-1-1.html 讲的很详细,我也不想重复。
这个Demo就不提供下载了,毕竟不是很难,所有的东西都交代了,自己敲一遍收获总是比打开别人的代码来研究要好的多。
例子中有什么错误的地方欢迎指正。