Fragment动态滑动

package com.example.day1;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;


import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Fragment01 f1;
    private Fragment02 f2;
    private Fragment03 f3;
    private Fragment04 f4;


    private RadioGroup ra;
    private FrameLayout fram;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ra = (RadioGroup) findViewById(R.id.rd);
        fram = (FrameLayout) findViewById(R.id.fram);


        List<Fragment> list = new ArrayList<Fragment>();
        f1 = new Fragment01();
        f2 = new Fragment02();
        f3 = new Fragment03();
        f4 = new Fragment04();
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
        final FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fram,list.get(0)).commit();








        ra.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.radio1:
                        fragmentManager.beginTransaction().replace(R.id.fram,f1).commit();
                        break;
                    case R.id.radio2:
                        fragmentManager.beginTransaction().replace(R.id.fram,f2).commit();
                        break;
                    case R.id.radio3:
                        fragmentManager.beginTransaction().replace(R.id.fram,f3).commit();
                        break;
                    case R.id.radio4:
                        fragmentManager.beginTransaction().replace(R.id.fram,f4).commit();
                        break;
                }
            }
        });
    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.example.administrator.myapplication.MainActivity01">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:id="@+id/fl"
        ></FrameLayout>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:id="@+id/rg">
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/rg1"
            android:button="@null"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="首页"
            android:background="@drawable/selector"
            android:checked="true"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/rg2"
            android:button="@null"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@drawable/selector"
            android:text="新闻"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/rg3"
            android:button="@null"
            android:text="我的"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@drawable/selector"
            />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/rg4"
            android:button="@null"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="咨询"
            android:background="@drawable/selector"
            />
    </RadioGroup>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值