android的RadioButton切换不同的fragment

来源:互联网 发布:php 比特比交易平台 编辑:程序博客网 时间:2024/05/16 05:17

MainActivity.java

package com.fragmentwechattest;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends Activity implements OnCheckedChangeListener {private FragmentTransaction beginTransaction;private FragmentManager fragmentManager;private WeFragment weFragment;private ContextFragment contextFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RadioGroup radioGroup=(RadioGroup) findViewById(R.id.my_radio_group);radioGroup.setOnCheckedChangeListener(this);fragmentManager = getFragmentManager();weFragment = new WeFragment();contextFragment = new ContextFragment();/*RadioButton radioButton = (RadioButton) radioGroup.getChildAt(0);radioButton.setChecked(true);*/RadioButton radioButton=(RadioButton) findViewById(R.id.my_radiobtn_we);radioButton.setChecked(true);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch(checkedId){case R.id.my_radiobtn_we://新开一个事务beginTransaction = fragmentManager.beginTransaction();beginTransaction.replace(R.id.frag_container,weFragment);beginTransaction.commit();break;case R.id.my_radiobtn_context:beginTransaction = fragmentManager.beginTransaction();beginTransaction.replace(R.id.frag_container,contextFragment);beginTransaction.commit();break;default:break;}}}

WeFragment.java

package com.fragmentwechattest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class WeFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.we_fragment, container, false);}}
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="com.fragmentwechattest.MainActivity" >    <FrameLayout         android:id="@+id/frag_container"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        ></FrameLayout>    <RadioGroup         android:id="@+id/my_radio_group"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_marginBottom="5dp"        >        <RadioButton             android:id="@+id/my_radiobtn_we"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:drawableTop="@drawable/we_selector"            android:gravity="center"            android:text="we"            android:button="@null"            />        <RadioButton             android:id="@+id/my_radiobtn_context"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:drawableTop="@drawable/context_selector"            android:gravity="center"            android:text="context"            android:button="@null"            />        <RadioButton             android:id="@+id/my_radiobtn_find"            android:layout_width="0dp"            android:layout_weight="1"            android:gravity="center"            android:layout_height="wrap_content"            android:drawableTop="@drawable/find_selector"            android:text="find"            android:button="@null"            />        <RadioButton             android:id="@+id/my_radiobtn_me"            android:layout_width="0dp"            android:layout_weight="1"            android:gravity="center"            android:layout_height="wrap_content"            android:drawableTop="@drawable/me_selector"            android:text="me"            android:button="@null"            />    </RadioGroup>    </LinearLayout>
content_frag.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"    android:background="#B4CDE6" >    <TextView     android:id="@+id/my_wechat_fragment"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="content"    /></LinearLayout>

we_fragment.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"    android:background="#f00" >    <TextView     android:id="@+id/my_wechat_fragment"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="wechart"    /></LinearLayout>




0 0