mainActivity

public int bindLayout() {
return R.layout.activity_main;
}

@Override
protected void initView() {
    radiogroup = findViewById(R.id.radioGroup);

}

@Override
protected void initData() {
    frag1 = new Frag1();
    frag2 = new Frag2();
    frag3 = new Frag3();

    //获取fragment管理者
    manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.pager,frag1)
            .add(R.id.pager,frag2)
            .add(R.id.pager,frag3)
            .commit();

}

@Override
protected void binEvent() {
    radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //开启事务
            FragmentTransaction transaction = manager.beginTransaction();
            switch (checkedId){
                case R.id.radio1:
                    transaction.show(frag1).hide(frag2).hide(frag3);
                    break;
                case R.id.radio2:
                    transaction.show(frag2).hide(frag1).hide(frag3);
                    break;
                case R.id.radio3:
                    transaction.show(frag3).hide(frag2).hide(frag1);
                    break;

            }
            transaction.commit();
        }
    });


}

}

你可能感兴趣的:(mainActivity)