package cn.flightfeather.thirdapp.module; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.bean.entity.Subtask; import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo; import cn.flightfeather.thirdapp.business.widgettype2.HomeFragment_comptent; import cn.flightfeather.thirdapp.business.widgettype3.HomeFragment_scene; import cn.flightfeather.thirdapp.dataanalysis.AnalysisOverViewFragment; import cn.flightfeather.thirdapp.fragment.HomeFragment; import cn.flightfeather.thirdapp.fragment.SettingFragment; import cn.flightfeather.thirdapp.fragment.TaskFragment; import cn.flightfeather.thirdapp.module.inspection.InspectionFragment; import cn.flightfeather.thirdapp.module.inspectioninfo.InspectionInfoFragment; import cn.flightfeather.thirdapp.module.inspectioninfo.ProblemChangeDetailActivity; import cn.flightfeather.thirdapp.util.Constant; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private LinearLayout ll_home; private LinearLayout ll_inspection_infos; private LinearLayout ll_task; private LinearLayout ll_inspection; private LinearLayout ll_anysis; private LinearLayout ll_setting; private List tabList; private List fragmentList = new ArrayList<>(); private FrameLayout fl_fragmentContainer; private FragmentTransaction fragmentTransaction; private Fragment fragmentCurrent; private InspectionFragment mInspectionFragment; private boolean exist = false; private CommonApplication app; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what ==0){ exist = false; } } }; // //定义菜单控制器对象数组 // private OnMenuController[] onMenuControllers; //广播 private MainReceiver mainReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); app = (CommonApplication) getApplication(); initUI(); switchSelectedFragment(ll_home); initBroadcast(); } /** * Dispatch onPause() to fragments. */ @Override protected void onPause() { super.onPause(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mainReceiver); } private void initUI() { ll_home = (LinearLayout) findViewById(R.id.ll_home); ll_inspection_infos = findViewById(R.id.ll_inspection_infos); ll_task = (LinearLayout) findViewById(R.id.ll_task); ll_inspection = (LinearLayout) findViewById(R.id.ll_inspection); ll_anysis = (LinearLayout) findViewById(R.id.ll_anysis); ll_setting = (LinearLayout) findViewById(R.id.ll_setting); fl_fragmentContainer = (FrameLayout) findViewById(R.id.fl_fragment_container); tabList = new ArrayList<>(); fragmentList.clear(); mInspectionFragment = new InspectionFragment(); //region 不同用户的主页不同 if (app.getCurrentUser().getUsertypeid() == 1 || app.getCurrentUser().getUsertypeid() == 0) {//公司内部人员 tabList.add(ll_home); // tabList.add(ll_inspection_infos); tabList.add(ll_task); tabList.add(ll_inspection); // tabList.add(ll_anysis); tabList.add(ll_setting); fragmentList.add(new HomeFragment()); fragmentList.add(new TaskFragment()); fragmentList.add(mInspectionFragment); // fragmentList.add(new AnysisFragment()); fragmentList.add(new SettingFragment()); ll_inspection_infos.setVisibility(View.GONE); ll_anysis.setVisibility(View.GONE); } else if (app.getCurrentUser().getUsertypeid() == 2) {//环保部门 tabList.add(ll_home); // tabList.add(ll_inspection_infos); // tabList.add(ll_task); tabList.add(ll_inspection); tabList.add(ll_anysis); tabList.add(ll_setting); fragmentList.add(new HomeFragment_comptent()); fragmentList.add(mInspectionFragment); fragmentList.add(AnalysisOverViewFragment.newInstance()); fragmentList.add(new SettingFragment()); ll_inspection_infos.setVisibility(View.GONE); ll_task.setVisibility(View.GONE); } else if (app.getCurrentUser().getUsertypeid() == 3) {//企业 tabList.add(ll_home); tabList.add(ll_inspection_infos); // tabList.add(ll_task); // tabList.add(ll_inspection); // tabList.add(ll_anysis); tabList.add(ll_setting); fragmentList.add(new HomeFragment_scene()); fragmentList.add(InspectionInfoFragment.newInstance()); fragmentList.add(new SettingFragment()); ll_task.setVisibility(View.GONE); ll_inspection.setVisibility(View.GONE); ll_anysis.setVisibility(View.GONE); } //endregion ll_home.setOnClickListener(this); ll_inspection_infos.setOnClickListener(this); ll_task.setOnClickListener(this); ll_inspection.setOnClickListener(this); ll_anysis.setOnClickListener(this); ll_setting.setOnClickListener(this); } private void initBroadcast(){ mainReceiver = new MainReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Constant.GOTOSUBTASK); filter.addAction(Constant.GOTO_INSPECTION_FRAGMENT); filter.addAction(Constant.REFRESH_DATA); registerReceiver(mainReceiver, filter); } private void switchSelectedFragment(LinearLayout ll) { for (LinearLayout ll_tab : tabList) { ll_tab.setSelected(false); } ll.setSelected(true); int position = tabList.indexOf(ll); if (position > -1) { fragmentTransaction = getSupportFragmentManager().beginTransaction(); if (!fragmentList.get(position).isAdded()) { if (fragmentCurrent != null) { fragmentTransaction.hide(fragmentCurrent); } fragmentTransaction.add(R.id.fl_fragment_container, fragmentList.get(position)); } else if (fragmentCurrent != fragmentList.get(position)) { fragmentTransaction.hide(fragmentCurrent); fragmentTransaction.show(fragmentList.get(position)); } fragmentCurrent = fragmentList.get(position); fragmentTransaction.commitAllowingStateLoss(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ll_home: switchSelectedFragment(ll_home); break; case R.id.ll_inspection_infos: switchSelectedFragment(ll_inspection_infos); break; case R.id.ll_task: switchSelectedFragment(ll_task); break; case R.id.ll_inspection: switchSelectedFragment(ll_inspection); break; case R.id.ll_anysis: switchSelectedFragment(ll_anysis); break; case R.id.ll_setting: switchSelectedFragment(ll_setting); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == Constant.REQUESTCODE_FROM_TASKFRAGMENT && requestCode == RESULT_OK) { final Subtask subtask = (Subtask) data.getSerializableExtra(Constant.KEY_INTENT_SUBTASK); ll_home.postDelayed(new Runnable() { @Override public void run() { switchSelectedFragment(ll_inspection); // mInspectionFragment.loadInspection(subtask); } }, 2000); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override public void onBackPressed() { if (exist){ Intent launcherIntent = new Intent(Intent.ACTION_MAIN); launcherIntent.addCategory(Intent.CATEGORY_HOME); startActivity(launcherIntent); // // finish(); }else { exist = true; Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessageDelayed(0,1000); } } class MainReceiver extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { switch (intent.getAction()){ case Constant.GOTOSUBTASK: final Subtask subtask = (Subtask) intent.getSerializableExtra("subtask"); if (subtask != null) { switchSelectedFragment(ll_inspection); ll_home.postDelayed(new Runnable() { @Override public void run() { // Log.e("Subtask", subtask.getStguid()); // if (fragmentCurrent instanceof InspectionFragment) // ((InspectionFragment) fragmentCurrent).loadInspection(subtask); // mInspectionFragment.loadInspection(subtask); // Log.e("Subtask2", subtask.getStguid()); } }, 2000); } break; case Constant.GOTO_INSPECTION_FRAGMENT: switchSelectedFragment(ll_inspection_infos); break; case Constant.REFRESH_DATA: if (app.getCurrentUser().getUsertypeid() == 3) {//企业用户 HomeFragment_scene h = (HomeFragment_scene) fragmentList.get(0); h.setUnHiddenRefresh(true); ProblemlistVo p = (ProblemlistVo) intent.getSerializableExtra(ProblemChangeDetailActivity.ARG_PROBLEM); InspectionInfoFragment i = (InspectionInfoFragment) fragmentList.get(1); i.updateData(); } break; } } } // /*** // * 根据传入的位置 给menu 控制器数组对象赋值 // * // * @param position // * @param onMenuController // */ // public void setOnMenuController(@IntRange(from = 0, to = 4) int position, OnMenuController onMenuController) { // onMenuControllers[position] = onMenuController; // invalidateOptionsMenu(); // } // // public interface OnMenuController { // /** // * 获取页面所加载的menu文件 // * // * @return // */ // int getMenuRes(); // // /** // * 页面menu创建的回调方法 // * // * @param menu // */ // void onMenuCreate(Menu menu); // // /*** // * menu 中每一项菜单选中的事件 // * // * @param item // */ // void onMenuItemSelected(MenuItem item); // // } }