当前位置: 代码迷 >> 综合 >> ViewPager,RadioGroup,FragmentManager 详解 一 主菜单 RadioGroup 控制显示Fragment ,FragmentManager
  详细解决方案

ViewPager,RadioGroup,FragmentManager 详解 一 主菜单 RadioGroup 控制显示Fragment ,FragmentManager

热度:88   发布时间:2023-12-16 15:45:21.0

0:首先创建3个 Fragment,这3个Fragment 必须是v4包里面的Fragment,

1:然后在MainActivity的布局里面添加一个FrameLayout和一个RadioGroup的布局,注意这个RadioGroup的布局一定要设置orientation 为horization

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:orientation="vertical"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><FrameLayout  android:layout_width="match_parent"  android:layout_height="0dp"  android:layout_weight="1"  android:id="@+id/framelayout"></FrameLayout><RadioGroup  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="horizontal"  android:id="@+id/radioGroup"><RadioButton  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="a"  android:textSize="35sp"  android:id="@+id/radiobutton_a"/><RadioButton  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="b"  android:textSize="35sp"  android:id="@+id/radiobutton_b"/><RadioButton  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="c"  android:textSize="35sp"  android:id="@+id/radiobutton_c"/></RadioGroup>
</LinearLayout>


2:在MainActivity 里面执行

package tech.androidstudio.viewpagerdemo;import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {private BlankFragment mBlankFragment;private SecondFragmentT mSecnodFragment;private ThridFragmentT mThirdFragment;private RadioGroup mRadioGroup;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//创建Fragment  mBlankFragment = new BlankFragment();mSecnodFragment = new SecondFragmentT();mThirdFragment = new ThridFragmentT();//获取支持包里面的FragmentManager,然后获取事务transaction  FragmentManager supportFragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();//将三个 Fragment 都添加到事务里面来,同时将后两个隐藏  fragmentTransaction.add(R.id.framelayout, mBlankFragment);fragmentTransaction.add(R.id.framelayout,mSecnodFragment);fragmentTransaction.add(R.id.framelayout,mThirdFragment);fragmentTransaction.hide(mSecnodFragment);fragmentTransaction.hide(mThirdFragment);//提交事务  fragmentTransaction.commit();//实现 RadioGroup 来控制FragmentManager的显示 ,设置监听器  mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);mRadioGroup.check(0);mRadioGroup.setOnCheckedChangeListener(this);}@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {//注意在回调方法里面 也要 获取FragmentManger 和事务transaction FragmentManager supportFragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();//判断checkedId 是要通过RadioButton 来判断的,如果是自己在代码里面创建的 RadioButton的话 可以设置id //例如设置012. 而这里是通过布局来创建的 ,所以 这里的 id 就使用布局里面的id就好了  switch (checkedId){case R.id.radiobutton_a:Toast.makeText(this,"0",Toast.LENGTH_LONG).show();fragmentTransaction.show(mBlankFragment);fragmentTransaction.hide(mSecnodFragment);fragmentTransaction.hide(mThirdFragment);break;case R.id.radiobutton_b:Toast.makeText(this,"1",Toast.LENGTH_LONG).show();fragmentTransaction.hide(mBlankFragment);fragmentTransaction.show(mSecnodFragment);fragmentTransaction.hide(mThirdFragment);break;case R.id.radiobutton_c:Toast.makeText(this,"2",Toast.LENGTH_LONG).show();fragmentTransaction.hide(mBlankFragment);fragmentTransaction.hide(mSecnodFragment);fragmentTransaction.show(mThirdFragment);break;}//别忘了提交事务  fragmentTransaction.commit();}
}



  相关解决方案