package com.shunzhi.parent.ui; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.MenuItem; import com.share.mvpsdk.base.activity.BaseCompatActivity; import com.share.mvpsdk.helper.BottomNavigationViewHelper; import com.share.mvpsdk.utils.PermissionUtils; import com.shunzhi.parent.AppContext; import com.shunzhi.parent.R; import com.shunzhi.parent.ui.fragment.CePingFragment; import com.shunzhi.parent.ui.fragment.ConsultFragment; import com.shunzhi.parent.ui.fragment.MineFragment; import com.shunzhi.parent.ui.fragment.ReportFragment; public class MainActivity extends BaseCompatActivity implements PermissionUtils.PermissionGrant { BottomNavigationView bottom_navigationView; CePingFragment cePingFragment = null; ConsultFragment consultFragment = null; ReportFragment reportFragment = null; MineFragment mineFragment = null; Fragment[] fragments = null; private int CEPING_INDEX = 0, CONSULT_INDEX = 1, REPORT_INDEX = 2, MINE_INDEX = 3; FragmentTransaction fragmentTransaction = null; @Override protected void initView(Bundle savedInstanceState) { PermissionUtils.requestMultiPermissions(this, this); bottom_navigationView = findViewById(R.id.bottom_navigationView); BottomNavigationViewHelper.disableShiftMode(bottom_navigationView); bottom_navigationView.setSelectedItemId(R.id.bottom_navigationView); bottom_navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_index://测评页面 showFragment(CEPING_INDEX); break; case R.id.menu_item_consult://咨询频道 showFragment(CONSULT_INDEX); break; case R.id.menu_item_report://报告 showFragment(REPORT_INDEX); break; case R.id.menu_item_mine://个人中心 showFragment(MINE_INDEX); break; } return true; } }); initFragments(); } private void initFragments() { fragments = new Fragment[4]; cePingFragment = new CePingFragment(); consultFragment = new ConsultFragment(); reportFragment = new ReportFragment(); mineFragment = new MineFragment(); fragments[CEPING_INDEX] = cePingFragment; fragments[CONSULT_INDEX] = consultFragment; fragments[REPORT_INDEX] = reportFragment; fragments[MINE_INDEX] = mineFragment; fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.frame, cePingFragment) .add(R.id.frame, consultFragment) .add(R.id.frame, reportFragment) .add(R.id.frame, mineFragment) .show(cePingFragment) .hide(consultFragment) .hide(reportFragment) .hide(mineFragment) .commit(); } private void showFragment(int index) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); for (int i = 0; i < fragments.length; i++) { if (index == i) fragmentTransaction.show(fragments[index]); else fragmentTransaction.hide(fragments[i]); } fragmentTransaction.commit(); } @Override protected int getLayoutId() { return R.layout.activity_main; } @Override protected void onDestroy() { super.onDestroy(); AppContext.getInstance().destoryLocation(); } @Override public void onPermissionGranted(int requestCode) { } }