使用FragmentTransaction对多个fragment进行切换

 通过record_car和record_person的点击实现下边的fragment的切换。

过程:

1.获取fragmentManager

2.获取FragmentTransaction

3.add/remove/hide/show等方法的使用,本例中使用的是add进去后,再通过使用show和hide进行切换,没有使用remove。

 

代码实例:

public class RecordTabActivity extends FragmentActivity implements View.OnClickListener {
    private TextView record_person;
    private TextView record_car;
    private ImageView back;
    private View person_view;
    private View car_view;
    private RecordPersonFragment recordPersonFragment;
    private RecordCarFragment recordCarFragment;
    FragmentManager fragmentManager;


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

    private void initview() {
        record_person = (TextView)findViewById(R.id.record_person);
        record_car = (TextView)findViewById(R.id.record_car);
        back = (ImageView)findViewById(R.id.head_blue_back);
        person_view = (View)findViewById(R.id.person_view);
        car_view = (View)findViewById(R.id.car_view);
        record_person.setOnClickListener(this);
        record_car.setOnClickListener(this);
        back.setOnClickListener(this);
        fragmentManager = getFragmentManager();
        record_person.performClick();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head_blue_back:
                finish();
                break;
            case R.id.record_person:
                setTabSelection(0);
                person_view.setVisibility(View.VISIBLE);
                car_view.setVisibility(View.INVISIBLE);
                break;
            case R.id.record_car:
                setTabSelection(1);
                person_view.setVisibility(View.INVISIBLE);
                car_view.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    private void setTabSelection(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index){
            case 0:
                if(recordPersonFragment == null){
                    recordPersonFragment = new RecordPersonFragment();
                    fragmentTransaction.add(R.id.record_fragment,recordPersonFragment);
                }else {
                    fragmentTransaction.show(recordPersonFragment);
                }
                break;
            case 1:
                if(recordCarFragment == null){
                    recordCarFragment = new RecordCarFragment();
                    fragmentTransaction.add(R.id.record_fragment,recordCarFragment);
                }else {
                    fragmentTransaction.show(recordCarFragment);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if(recordPersonFragment != null){
            fragmentTransaction.hide(recordPersonFragment);
        }
        if(recordCarFragment != null){
            fragmentTransaction.hide(recordCarFragment);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值