RadioGroup和FrameLayout联动+TabLayout和ViewPager2

RadioGroup的activity布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".conterllor.MainActivity">


    <FrameLayout
        android:id="@+id/fra"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"/>
    <RadioGroup
        android:id="@+id"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <RadioButton
            android:id="@+id/rb"
            android:text="星球"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/img"
            android:checked="false"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"/>
        <RadioButton
            android:id="@+id/rb2"
            android:text="广场"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/img2"
            android:checked="true"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_height="match_parent"
            android:button="@null"
            android:text="发布瞬间"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/ic_fb"/>
        <RadioButton
            android:id="@+id/rb3"
            android:text="聊天"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/img3"
            android:checked="false"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"/>
        <RadioButton
            android:id="@+id/rb4"
            android:text="自己"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/img4"
            android:checked="false"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"/>

    </RadioGroup>

RadioGroup的主页面代码实现

private RadioGroup radioGroup;
    private AFragment aFragment;
    private TuiJianFragment bFragment;
    private CFragment cFragment;
    private DFragment dFragment;
    private GuangChangFragment fcFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=findViewById(R.id.rg);
        aFragment=new AFragment();
        bFragment=new TuiJianFragment();
        cFragment=new CFragment();
        dFragment=new DFragment();
        fcFragment=new GuangChangFragment();
        FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
        beginTransaction.replace(R.id.fra,aFragment);
        beginTransaction.commit();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb:
                        abc(aFragment);
                        break;
                    case R.id.rb2:
                        abc(fcFragment);
                        break;
                    case R.id.rb3:
                        abc(cFragment);
                        break;
                    case R.id.rb4:
                        abc(dFragment);
                        break;
                    default:break;
                }
            }
        });
    }
    public void abc(Fragment fragment){
        FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
        beginTransaction.replace(R.id.fra,fragment);
        beginTransaction.commit();
    }
ViewPager2布局:
  <com.google.android.material.tabs.TabLayout
        android:id="@+id/vp1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.google.android.material.tabs.TabLayout>
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></androidx.viewpager2.widget.ViewPager2>

manactivity:

  private ViewPager2 vp2;
    private TabLayout vp1;
    private List<Fragment> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();

       list.add(new FragmentA());
       list.add(new FragmentB());
       list.add(new FragmentC());

         vp2.setAdapter(new Mydel(this));
        TabLayoutMediator mediator = new TabLayoutMediator(vp1, vp2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
              if (position==0){
                  tab.setText("广场");
              }else if (position==1){
                  tab.setText("聊天");
              }else {
                  tab.setText("自己");
              }
            }
        });
        mediator.attach();
    }
    //适配器
    class Mydel extends FragmentStateAdapter {

        public Mydel(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return list.get(position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }
    private void initView() {
        vp2 = (ViewPager2) findViewById(R.id.vp2);
        vp1 = (TabLayout) findViewById(R.id.vp1);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值