RadioGroup和Frame实现底部tab切换高仿微信底部FragmentTransaction事务

底部功能切换简单实现。

先上图,有图才有真相


是采用 FrameLayout 和 RadioGrop 分组 RadioButton 这些控件实现。

做界面的切换,定义5个不同的界面,当然也可以自定义一个,5公用1 的意思,都是继承 Frament 类

再做一个XML

在Activity 里实现关键代码 ,getSupportFragmentManager()方法,获取Fragment管理对象。

FragmentTransaction 事务,使用提供的方法 show()显示    和  hide () 隐藏   commit() 提交 。还有一种方式采用 replace() 方法替换

废话少说,上代码



package song.com.cn.imagebig.radiogroup;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;

import song.com.cn.imagebig.R;

/**
 * @author song
 * @date :2017/6/14
 * @Description:
 */
public class RadiogroupActivity extends FragmentActivity implements View.OnClickListener {

    private RadioButton shouye, maimai, chat, shop, geren;
    private CharFrament charFrament;
    private GerenFrament gerenFrament;
    private MaimaiFrament maimaiFrament;
    private ShopFrament shopFrament;
    private ShouFrament shouFrament;
    private FragmentManager fManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radiogroup);
        shouye = (RadioButton) findViewById(R.id.shou_ye_rb);
        maimai = (RadioButton) findViewById(R.id.shou_ye_rb1);
        chat = (RadioButton) findViewById(R.id.shou_ye_rb2);
        shop = (RadioButton) findViewById(R.id.shou_ye_rb3);
        geren = (RadioButton) findViewById(R.id.shou_ye_rb4);
        fManager = getSupportFragmentManager();
        initOnClick();
    }

    private void initOnClick() {
        shouye.setOnClickListener(this);
        maimai.setOnClickListener(this);
        chat.setOnClickListener(this);
        shop.setOnClickListener(this);
        geren.setOnClickListener(this);
        //开启事务,fragment的控制是由事务来实现的
        initFragment();
    }

    //显示第一个fragment
    private void initFragment() {
        //开启事务,fragment的控制是由事务来实现的
        FragmentTransaction transaction = fManager.beginTransaction();
        if (shouFrament == null) {
            shouFrament = new ShouFrament();
            transaction.add(R.id.main_frame_layout, shouFrament);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(shouFrament);
        //提交事务
        transaction.commit();
    }

    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (shouFrament != null) {
            transaction.hide(shouFrament);
        }
        if (maimaiFrament != null) {
            transaction.hide(maimaiFrament);
        }
        if (charFrament != null) {
            transaction.hide(charFrament);
        }
        if (shopFrament != null) {
            transaction.hide(shopFrament);
        }
        if (gerenFrament != null) {
            transaction.hide(gerenFrament);
        }
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = fManager.beginTransaction();
        //隐藏所有fragment
        hideFragment(transaction);
        switch (v.getId()) {
            case R.id.shou_ye_rb:
                if (shouFrament == null) {
                    shouFrament = new ShouFrament();
                    transaction.add(R.id.main_frame_layout, shouFrament);
                }
                //显示需要显示的fragment
                transaction.show(shouFrament);
                break;
            case R.id.shou_ye_rb1:
                if (maimaiFrament == null) {
                    maimaiFrament = new MaimaiFrament();
                    transaction.add(R.id.main_frame_layout, maimaiFrament);
                }

                //显示需要显示的fragment
                transaction.show(maimaiFrament);
                break;
            case R.id.shou_ye_rb2:
                if (charFrament == null) {
                    charFrament = new CharFrament();
                    transaction.add(R.id.main_frame_layout, charFrament);
                }
                //显示需要显示的fragment
                transaction.show(charFrament);
                break;
            case R.id.shou_ye_rb3:
                if (shopFrament == null) {
                    shopFrament = new ShopFrament();
                    transaction.add(R.id.main_frame_layout, shopFrament);
                }
                //显示需要显示的fragment
                transaction.show(shopFrament);
                break;
            case R.id.shou_ye_rb4:
                if (gerenFrament == null) {
                    gerenFrament = new GerenFrament();
                    transaction.add(R.id.main_frame_layout, gerenFrament);
                }
                //显示需要显示的fragment
                transaction.show(gerenFrament);
                break;
        }
        transaction.commit();
    }
}


代码没有上传完,可以去下载


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="song.com.cn.imagebig.radiogroup.RadiogroupActivity">

    <FrameLayout
        android:id="@+id/main_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="55dp"
        android:layout_weight="1"
        android:background="#dddddd"></FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/radio_group"
        android:background="@color/colorAccent" />

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/shou_ye_rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/shou_ye_chek3"
            android:gravity="center_horizontal|bottom"
            android:text="首页" />

        <RadioButton
            android:id="@+id/shou_ye_rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/shou_ye_chek1"
            android:gravity="center_horizontal|bottom"
            android:text="买卖" />

        <RadioButton
            android:id="@+id/shou_ye_rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/shou_ye_chek2"
            android:gravity="center_horizontal|bottom"
            android:text="聊天" />

        <RadioButton
            android:id="@+id/shou_ye_rb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/shou_ye_chek4"
            android:gravity="center_horizontal|bottom"
            android:text="市场" />

        <RadioButton
            android:id="@+id/shou_ye_rb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/shou_ye_chek"
            android:gravity="center_horizontal|bottom"
            android:text="个人" />

    </RadioGroup>
</RelativeLayout>



结束,觉得好就顶一下,觉得不好就吐槽,改进改进


源码下载:

http://download.csdn.net/detail/qq_33495943/9870374

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值