RadioGroup点击事件切换fragement

比如qq等手机界面点开之后,下面的导航栏就是单选按钮,每个按钮切换的就是fragment

 

Main_activity.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" >

    <LinearLayout
        android:id="@+id/fragement"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioGroup
            android:id="@+id/index_radiogroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/rb_index_selector"
                android:gravity="center"
                android:text="首页"
                android:textColor="@color/index_rb_textcolor_selector"
                android:textSize="10sp" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/rb_message_selector"
                android:gravity="center"
                android:text="消息"
                android:textColor="@color/index_rb_textcolor_selector"
                android:textSize="10sp" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:button="@null"
                android:gravity="center"
                android:src="@drawable/rb_add_selector"
                android:textSize="10sp" />

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/rb_discover_selector"
                android:gravity="center"
                android:text="发现"
                android:textColor="@color/index_rb_textcolor_selector"
                android:textSize="10sp" />

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/rd_me_selector"
                android:gravity="center"
                android:text="我"
                android:textColor="@color/index_rb_textcolor_selector"
                android:textSize="10sp" />
        </RadioGroup>
    </LinearLayout>

MainActivity.java

 

 

 

 

 

 

MainActivity.java

 

public class MainActivity extends FragmentActivity {
	RadioGroup radio;// 导航栏的图标
	FragmentManager manager;
	FragmentTransaction trans;
	// 四个fragment
	FaxianFragment fragment_faxian;
	IndexFragment fragment_index;
	MessageFragment fragment_message;
	MyFragment fragment_my;
	// 装fragment的布局位置
	LinearLayout ly;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_layout);

		ly = (LinearLayout) findViewById(R.id.fragement);

		radio = (RadioGroup) findViewById(R.id.index_radiogroup);
		radio.setOnCheckedChangeListener(listener);

		manager = getSupportFragmentManager();// 得到FragmentManager对象
		trans = manager.beginTransaction();
		// 初始化fragment
		fragment_faxian = new FaxianFragment();
		fragment_index = new IndexFragment();
		fragment_message = new MessageFragment();
		fragment_my = new MyFragment();

//先加载首页,及每次进入的默认界面
		trans.add(R.id.fragement, fragment_index);
		trans.commit();

	}

	OnCheckedChangeListener listener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkId) {
			trans = manager.beginTransaction();
//trans.commit();使用时候,trans就不能用了,所以必须每次都要获取
			switch (checkId) {
			case R.id.radio1:
				trans.replace(R.id.fragement, fragment_index);
				break;
			case R.id.radio2:
				trans.replace(R.id.fragement, fragment_message);
				break;
			case R.id.radio3:
				trans.replace(R.id.fragement, fragment_faxian);
				break;
			case R.id.radio4:
				trans.replace(R.id.fragement, fragment_my);
				break;
			default:
				break;
			}
			trans.commit();//必须提交Transaction
		}
	};
}


每个fragment里面目前只加载一个页面

FaxianFragment.java

 

public class FaxianFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	View view=inflater.inflate(R.layout.activity_faxian, container,false);
	return view;
}
}


 

IndexFragment.java

 

public class IndexFragment extends Fragment {

	@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	View view=inflater.inflate(R.layout.activity_index, container,false);
	return view;
}
}


 

MessageFragment.java

 

public class MessageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	View view=inflater.inflate(R.layout.activity_message, container, false);
	return view;
}
}


 

MyFragment.java

 

public class MyFragment extends Fragment {
	Button btn_setting;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_my, container, false);
		return view;
	}
}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(RadioGroup点击事件切换fragement)