Android简单实现Fragment

fragment切换页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".activity.Home_page_Activity">
    <FrameLayout
        android:id="@+id/main_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>
    <RadioGroup
        android:padding="@dimen/dp_10"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rectangle"
        android:layout_height="@dimen/dp_120">
        <RadioButton
            android:id="@+id/rb_show"
            android:checked="true"
            style="@style/main_bottom_btn_style"
            android:drawableTop="@drawable/home_page_selector1"/>
        <RadioButton
            android:id="@+id/rb_circle"
            style="@style/main_bottom_btn_style"
            android:drawableTop="@drawable/home_page_selector2"/>
        <RadioButton
            android:id="@+id/rb_shopcar"
            style="@style/main_bottom_btn_style"
            android:layout_marginTop="0dp"
            android:drawableTop="@drawable/shopping_two"
            android:layout_marginLeft="-3dp" />
        <RadioButton
            android:id="@+id/rb_list"
            style="@style/main_bottom_btn_style"
            android:drawableTop="@drawable/home_page_selector3"/>
        <RadioButton
            android:id="@+id/rb_mine"
            style="@style/main_bottom_btn_style"
            android:drawableTop="@drawable/home_page_selector4"/>
    </RadioGroup>
</RelativeLayout>

切换的代码

@Override
    protected void initView() {
        one = new frag_01();
        two = new frag_02();
        three = new frag_03();
        four = new frag_04();
        five = new frag_05();
        final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.main_frameLayout, one);
        transaction.add(R.id.main_frameLayout, two);
        transaction.add(R.id.main_frameLayout, three);
        transaction.add(R.id.main_frameLayout, four);
        transaction.add(R.id.main_frameLayout, five);
        //进行显隐切换
        transaction.show(one).hide(two).hide(three).hide(four).hide(five).commit();
        FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
        transaction1.show(one).hide(two).hide(three).hide(four).hide(five).commit();
    }

点击事件的切换

@OnClick(R.id.rb_show)
    public void show() {
        FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
        transaction1.show(one).hide(two).hide(three).hide(four).hide(five).commit();
    }
    @OnClick(R.id.rb_circle)
    public void circle() {
        FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
        transaction2.show(two).hide(one).hide(three).hide(four).hide(five).commit();
    }
    @OnClick(R.id.rb_shopcar)
    public void shopcar() {
        FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
        transaction3.show(three).hide(two).hide(one).hide(four).hide(five).commit();
    }
    @OnClick(R.id.rb_list)
    public void list() {
        FragmentTransaction transaction4 = getSupportFragmentManager().beginTransaction();
        transaction4.show(four).hide(two).hide(one).hide(three).hide(five).commit();
    }
    @OnClick(R.id.rb_mine)
    public void mine() {
        FragmentTransaction transaction5 = getSupportFragmentManager().beginTransaction();
        transaction5.show(five).hide(two).hide(one).hide(three).hide(four).commit();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值