public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bu_delete;
private Button bu_01;
private Button bu_02;
private Button bu_03;
private Frag1 frag1;
private Frag2 frag2;
private Frag3 frag3;
private ViewPager vp;
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bu_delete = (Button) findViewById(R.id.bu_delete);
bu_01 = (Button) findViewById(R.id.bu_01);
bu_02 = (Button) findViewById(R.id.bu_02);
bu_03 = (Button) findViewById(R.id.bu_03);
bu_01.setOnClickListener(this);
bu_02.setOnClickListener(this);
bu_03.setOnClickListener(this);
frag1 = new Frag1();
frag2 = new Frag2();
frag3 = new Frag3();
//清除缓存的监听
bu_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageLoader.getInstance().clearMemoryCache();
ImageLoader.getInstance().clearDiskCache();
}
});
manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.frame, frag1);
transaction.add(R.id.frame, frag2);
transaction.add(R.id.frame, frag3);
showOrHide(frag1, frag2, frag3);
transaction.commit();
}
//fragment的显示与隐藏
private void showOrHide(Fragment from, Fragment to1, Fragment to2) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.show(from);
transaction.hide(to1);
transaction.hide(to2);
transaction.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bu_01:
showOrHide(frag1, frag2, frag3);
break;
case R.id.bu_02:
showOrHide(frag2, frag1, frag3);
break;
case R.id.bu_03:
showOrHide(frag3, frag1, frag2);
break;
}
}
}