底部四个按钮的设置+fragment的切换

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Fragment fm;
    private Home_Fragment fragment;
    private Class_Fragment fragment2;
    private Shopping_Fragment fragment3;
    private Myebuy_Fragment fragment4;
    private RadioButton rd1;
    private RadioButton rd2;
    private RadioButton rd3;
    private RadioButton rd4;
    private FragmentManager manager;
    private FragmentTransaction transaction;


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

        initView();
        //判断home_pager是否为null
        if (fragment == null) {
            fragment = new Home_Fragment();
            //设置首页默认被选中
            rd1.setChecked(true);
        }
        
        changeFragment(fragment);

    }

    


    private void initView() {

        rd1 = (RadioButton) findViewById(R.id.b1);
        rd2 = (RadioButton) findViewById(R.id.b2);
        rd3 = (RadioButton) findViewById(R.id.b3);
        rd4 = (RadioButton) findViewById(R.id.b4);
        rd1.setOnClickListener(this);
        rd2.setOnClickListener(this);
        rd3.setOnClickListener(this);
        rd4.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.b1:
            {
                if (fragment==null){
                    fragment=new Home_Fragment();
                }
                changeFragment(fragment);
            }
            break;

            case R.id.b2:
            {
                if (fragment2==null){
                    fragment2=new Class_Fragment();
                }
                changeFragment(fragment2);
            }
            break;

            case R.id.b3:
            {
                if (fragment3==null){
                    fragment3=new Shopping_Fragment();
                }
                changeFragment(fragment3);
            }
            break;

            case R.id.b4:
            {
                if (fragment4==null){
                    fragment4=new Myebuy_Fragment();
                }
                changeFragment(fragment4);
            }
            break;

        }

    }
    //切换fragment
    public void changeFragment(Fragment ment){

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        if (fm !=null){
            transaction.hide(fm);
        }
        // isAdded:是否被添加过 被添加过 is true 反之
        if (!ment.isAdded()) {
            // 调用replace 添加fragment
            transaction.add(R.id.fl, ment);
        } else {
            // 显示出来
            transaction.show(ment);
        }

        // 提交
        transaction.commit();

        // 记录当前的Fragment
        fm = ment;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值