Fragment布局

public class MyActivity extends FragmentActivity implements OnClickListener {

    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private FragmentManager supportFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除标题头
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_my);

        TextView my_tv1 = (TextView) findViewById(R.id.my_tv1);
        TextView my_tv2 = (TextView) findViewById(R.id.my_tv2);
        TextView my_tv3 = (TextView) findViewById(R.id.my_tv3);
        TextView my_tv4 = (TextView) findViewById(R.id.my_tv4);
        
        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager
                .beginTransaction();
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        transaction.add(R.id.my_frag1, fragment1, "fragment1");
        transaction.add(R.id.my_frag1, fragment2, "fragment2");
        transaction.add(R.id.my_frag1, fragment3, "fragment3");
        transaction.add(R.id.my_frag1, fragment4, "fragment4");
        transaction.commit();
        seitchHide("fragment1");
        my_tv1.setOnClickListener(this);
        my_tv2.setOnClickListener(this);
        my_tv3.setOnClickListener(this);
        my_tv4.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case R.id.my_tv1:
            seitchHide("fragment1");
            break;
        case R.id.my_tv2:
            seitchHide("fragment2");
            break;
        case R.id.my_tv3:
            seitchHide("fragment3");
            break;
        case R.id.my_tv4:
            seitchHide("fragment4");
            break;

        default:
            break;
        }

    }

    private void seitchHide(String hide) {
        FragmentTransaction transaction = supportFragmentManager
                .beginTransaction();
        if (hide.equals("fragment1")) {
            transaction.show(fragment1);
            transaction.hide(fragment2);
            transaction.hide(fragment3);
            transaction.hide(fragment4);
        } else if (hide.equals("fragment2")) {
            transaction.show(fragment2);
            transaction.hide(fragment1);
            transaction.hide(fragment3);
            transaction.hide(fragment4);
        } else if (hide.equals("fragment3")) {
            transaction.show(fragment3);
            transaction.hide(fragment1);
            transaction.hide(fragment2);
            transaction.hide(fragment4);
        } else if (hide.equals("fragment4")) {
            transaction.show(fragment4);
            transaction.hide(fragment3);
            transaction.hide(fragment2);
            transaction.hide(fragment1);
        }

        transaction.commit();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值