内部写了几个方法,比较方便使用。
public abstract classMFragmentActivityextendsFragmentActivity {
privateFragmentManagermFragmentManager;
privateListclassList=newArrayList<>();
privateListfragmentList=newArrayList<>();
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MActivityManager.getInstance().pushActivity(this);
//setTheme(R.style.AppTheme);
requestWindowFeature(Window.FEATURE_NO_TITLE);//沉浸式
setContentView(initContentView());
initState();//沉浸式
initView();
mFragmentManager= getFragmentManager();
initData();
initEvent();
}
/**
*@time2017/4/1 18:03
*@desc沉浸式
*/
private voidinitState() {
ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
View parentView = contentFrameLayout.getChildAt(0);
if(parentView !=null&& Build.VERSION.SDK_INT>=14) {
parentView.setFitsSystemWindows(true);
}
StatusBarCompat.compat(this);
}
@Override
protected voidonDestroy() {
super.onDestroy();
MActivityManager.getInstance().popActivity(this);
}
/**
*@time2017/7/11 11:36
*@desc返回键监听
*/
@Override
public voidonBackPressed() {
backPress();
}
@Override
public booleanonKeyDown(intkeyCode,KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK)) {
returnbackPress();
}else{
return super.onKeyDown(keyCode,event);
}
}
protected booleanbackPress() {
super.onBackPressed();
return false;
}
/**
*@time2017/7/11 11:40
*@desc Fragment切换方法,推荐
*/
public voidshowPager(Class c) {
try{
if(classList.contains(c)) {
inti =classList.indexOf(c);
Fragment fragment =fragmentList.get(i);
hideAllOfFragment();
showFragment(fragment);
}else{
classList.add(c);
Fragment fragment = (Fragment) c.newInstance();
fragmentList.add(fragment);
addFragment(fragment,c+"");
hideAllOfFragment();
showFragment(fragment);
}
}catch(Exception e) {
e.printStackTrace();
}
}
public voidhideAllOfFragment(){
for(Fragment fragment:fragmentList){
hideFragment(fragment);
}
}
/**
*@time2017/2/21 10:14
*@desc显示Fragment的方法
*/
public voidreplaceFragment(Fragment fragment) {
mFragmentManager.beginTransaction().replace(R.id.fl_fragment_activity_base,fragment).commit();
}
public voidaddFragment(Fragment fragment) {
mFragmentManager.beginTransaction().add(R.id.fl_fragment_activity_base,fragment).commit();
}
public voidaddFragment(Fragment fragment,String tag) {
mFragmentManager.beginTransaction().add(R.id.fl_fragment_activity_base,fragment,tag).commit();
}
public voidattachFragment(Fragment fragment) {
mFragmentManager.beginTransaction().attach(fragment).commit();
}
public voidhideFragment(Fragment fragment) {
mFragmentManager.beginTransaction().hide(fragment).commit();
}
public voiddetachFragment(Fragment fragment) {
mFragmentManager.beginTransaction().detach(fragment).commit();
}
public voidremoveFragment(Fragment fragment) {
mFragmentManager.beginTransaction().remove(fragment).commit();
}
public voidshowFragment(Fragment fragment) {
mFragmentManager.beginTransaction().show(fragment).commit();
}
/**
*@time2017/7/11 11:38
*@desc不需要处理
*/
protected intinitContentView() {
returnR.layout.fragment_activity_base;
}
protected voidinitView() {
}
protected abstract voidinitData();
protected voidinitEvent() {
}
/**
*@time2017/4/14 9:40
*@desc跳转到activity
*/
protected voidtoActivity(Class c, booleanclearOther,HashMap map) {
IntentUtils.makeIntent(this,c,clearOther,map);
}
}