private void initData() { fragmentManager = getSupportFragmentManager(); fragment1 = new Fragment1(); fragment2 = new Fragment2(); fragment3 = new Fragment3(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.main_linearLayout, fragment1, "fragment1"); fragmentTransaction.add(R.id.main_linearLayout, fragment2, "fragment2"); fragmentTransaction.add(R.id.main_linearLayout, fragment3, "fragment3"); fragmentTransaction.show(fragment1); //fragmentTransaction.hide(fragment2); // fragmentTransaction.hide(fragment3); fragmentTransaction.commit(); } private void initView() { textView1 = (TextView) findViewById(R.id.text_1); textView2 = (TextView) findViewById(R.id.text_2); textView3 = (TextView) findViewById(R.id.text_3); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cutFragment(fragment1, fragment2, fragment3); } }); textView2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cutFragment(fragment2, fragment1, fragment3); } }); textView3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cutFragment(fragment3, fragment1, fragment2); } }); } private void cutFragment(Fragment fragment1, Fragment fragment2, Fragment fragment3) { FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.show(fragment1); transaction.hide(fragment2); transaction.hide(fragment3); transaction.commit(); } }
fragment的管理
最新推荐文章于 2024-08-08 07:49:09 发布