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();
}
}
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();
}
}