Android-FrameLayout实现多页面(QQ页面)

第一步 在activity_main.xml中布局

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    //FrameLayout
    <FrameLayout
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    //底部按钮
    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:gravity="center"
            android:textSize="25dp"
            android:background="@drawable/selector"
            android:text="首页"/>
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="25dp"
            android:button="@null"
            android:background="@drawable/selector"
            android:text="推荐"/>
        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:textSize="25dp"
            android:background="@drawable/selector"
            android:text="我的"/>
    </RadioGroup>
</LinearLayout>

第二步 在MainActivity.java中写代码
public class MainActivity extends AppCompatActivity {


    private RadioGroup radiogroup;
    private FragmentManager manager;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;

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

        //查找控件
        radiogroup = (RadioGroup)findViewById( R.id.radiogroup );
        //得到管理这类
        manager = getSupportFragmentManager();
        //开启事物
        FragmentTransaction transaction = manager.beginTransaction();
        //添加事物
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        transaction.add( R.id.pager, fragment1 );
        transaction.add( R.id.pager, fragment2 );
        transaction.add( R.id.pager, fragment3 );
        //只显示第一个fragment
        transaction.show( fragment1 ).hide( fragment2 ).hide( fragment3 );
        //默认选中第一个按钮
        radiogroup.check( radiogroup.getChildAt( 0 ).getId() );
        //提交事物
        transaction.commit();

        //当我点击按钮时  页面切换
        radiogroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction1 = manager.beginTransaction();
                switch (checkedId){
                    case R.id.radio1:
                        //显示对应的fragment
                        transaction1.show( fragment1 ).hide( fragment2 ).hide( fragment3 );
                        break;
                    case R.id.radio2:
                        //显示对应的fragment
                        transaction1.show( fragment2 ).hide( fragment1 ).hide( fragment3 );
                        break;
                    case R.id.radio3:
                        //显示对应的fragment
                        transaction1.show( fragment3 ).hide( fragment2 ).hide( fragment1 );
                        break;
                }
                //提交事务
                transaction1.commit();
            }
        } );//当我点击按钮时  页面切换
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值