推荐一款fragment管理器——fragmentswitcher
开源地址:https://github.com/jacobtabak/Fragment-Switcher
在实际开发当中,有一种很常见的需求:底部栏有几个按钮,通过这些按钮来切换当前界面,思路有很多种:
1,radio group+view
2,radio group+viewpager
3,radio group+fragment
4,......
如果各个界面的数据相对独立,并且有一定的信息量时,最好是用fragment啦,方便数据管理,代码也好维护,不至于太乱。
fragmentswiter用法也很简单:
第一步:初始化各个fragment,放进去适配器FragmentStateArrayPagerAdapter里面。
private void initFragmentSwitcher() {
fragmentAdapter = new FragmentStateArrayPagerAdapter(getSupportFragmentManager());
fragmentSwitcher = (FragmentSwitcher) findViewById(R.id.fragment_container);
fragmentSwitcher.setAdapter(fragmentAdapter);
AFragment aFragment = AFragment.newInstance();
BFragment bFragment = BFragment.newInstance();
CFragment cFragment=CFragment.getInstance();
DFragment dFragment= D<span style="font-family: Arial, Helvetica, sans-serif;">Fragment.getInstance();</span>
fragmentAdapter.add(aFragment);
fragmentAdapter.add(bFragment);
fragmentAdapter.add(cFragment);
fragmentAdapter.add(dFragment);
fragmentSwitcher.setCurrentItem(0);
lastSelect = 0;
}
第二部:radioGroup控制fragment切换
private void initRadioGroup() {
radioGroup = (RadioGroup) findViewById(R.id.rgMainTab);
rb_home = (RadioButton) findViewById(R.id.rbMainTabHome);
rb_bbs = (RadioButton) findViewById(R.id.rbMainTabBbs);
rb_biao = (RadioButton) findViewById(R.id.rbMainTabMore);
rb_my = (RadioButton) findViewById(R.id.rbMainTabMy);
rb_home.setOnClickListener(this);
rb_bbs.setOnClickListener(this);
rb_biao.setOnClickListener(this);
rb_my.setOnClickListener(this);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rbMainTabHome://home
fragmentSwitcher.setCurrentItem(0);
setActionBarImage(R.drawable.logo);
<span style="white-space:pre"> </span>lastSelect = 0;
break;
case R.id.rbMainTabBbs://论坛
fragmentSwitcher.setCurrentItem(1);
lastSelect = 1;
break;
case R.id.rbMainTabMore://标
fragmentSwitcher.setCurrentItem(2);
lastSelect = 2;
break;
case R.id.rbMainTabMy://我的
if (Config.isLogin) {
fragmentSwitcher.setCurrentItem(3);
lastSelect = 3;
} else {
MainActivity.isLoginFromMain = true;
UIHelper.showLogin(MainActivity.this);
}
break;
}
}
});
}
第三部:按照一般需求,我们会在activity onresume时,自动切换到最后一次看到的fragment
@Override
protected void onResume() {
super.onResume();
turnToMain();
if (!Config.isLogin) {
if (Config.GESTURE_VERIFY_ERROR) {
UIHelper.showLogin(MainActivity.this);
return;
}
SharedPreferences preferences = getSharedPreferences(Config.PREFERENCE_NAME_LOCK, MODE_PRIVATE);
boolean isLock = preferences.getBoolean(Config.REMEBER_LOCK_LOGIN, false);
if (isLock) {
UIHelper.showGestureVerify(MainActivity.this);
MainActivity.this.finish();
}
}
getAppVersion(true);
switch (lastSelect) {
case 0:
fragmentSwitcher.setCurrentItem(0);
setActionBarImage(R.drawable.logo);
lastSelect = 0;
((RadioButton) radioGroup.getChildAt(lastSelect)).setChecked(true);
break;
case 1:
fragmentSwitcher.setCurrentItem(1);
lastSelect = 1;
fl_more.setVisibility(View.INVISIBLE);
fragmentSwitcher.setVisibility(View.VISIBLE);
((RadioButton) radioGroup.getChildAt(lastSelect)).setChecked(true);
case 2:
fragmentSwitcher.setCurrentItem(2);
lastSelect = 2;
fl_more.setVisibility(View.INVISIBLE);
fragmentSwitcher.setVisibility(View.VISIBLE);
((RadioButton) radioGroup.getChildAt(lastSelect)).setChecked(true);
break;
case 3:
fragmentSwitcher.setCurrentItem(3);
lastSelect = 3;
fl_more.setVisibility(View.INVISIBLE);
fragmentSwitcher.setVisibility(View.VISIBLE);
((RadioButton) radioGroup.getChildAt(lastSelect)).setChecked(true);
break;
}
}
大功告成!
相比起最原始的fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.fl_fragment,homeFragment).commit();是不是简便很多?不仅如此,它还帮我们剩下很多功夫,谁用谁知道。