@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
Class aclass = null;
Bundle args = new Bundle();
Intent intentOperatore = getIntent();
String mParamOperatore = intentOperatore.getStringExtra(ARG_PARAM_OPERATORE);
boolean navigation = Boolean.FALSE;
boolean intent = Boolean.FALSE;
boolean scan = Boolean.FALSE;
boolean exit = Boolean.FALSE;
try{
switch (item.getItemId()){
case R.id.nav_home:
aclass = HomeFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_PARAM_OPERATORE,mParamOperatore);
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_procedura_immobiliare:
aclass = FiltroFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_CHECK_CLASS,"immobiliari");
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_procedura_mobiliare:
aclass = FiltroFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_CHECK_CLASS,"mobiliari");
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_localizza_procedura:
aclass = LocalizzaActivity.class;
intent = Boolean.TRUE;
break;
case R.id.nav_ricognizione:
aclass = RicognizioneFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_PARAM_RICOGNIZIONE,""); //TODO: Add param here.
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_qrcode:
scan = Boolean.TRUE;
break;
case R.id.nav_exit:
exit = Boolean.TRUE;
break;
}
if(navigation) { // Navigazione per i fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
} if(intent) { // Navigazione per le activity
Intent myIntent = new Intent(getApplicationContext(),aclass);
myIntent.putExtra(ARG_PARAM_OPERATORE,mParamOperatore);
startActivity(myIntent);
finish();
} if(scan) {
new MVBarcodeScanner.Builder()
.setScanningMode(MVBarcodeScanner.ScanningMode.SINGLE_AUTO)
.setFormats(Barcode.QR_CODE)
.build()
.launchScanner(MainActivity.this,REQ_CODE);
} if(exit) { // Navigazione per l'uscita
MaterialDialog dialog = new MaterialDialog.Builder(this)
.title(R.string.title_exit)
.content(R.string.summary_exit)
.positiveText(R.string.action_exit)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
finish();
}
})
.negativeText(R.string.action_no)
.show();
}
} catch (Exception e) {
Log.e(this.getClass().getName(), e.getMessage(), e);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
编辑:
修改2: -我添加了onCreateOptionsMenu和一些在 onCreateView:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflater.inflate(R.menu.procedura_menu_action, menu);
inflater.inflate(R.menu.procedura_menu_list, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_procedure_list, container, false);
Context context = view.getContext();
setHasOptionsMenu(true);
recyclerView = (RecyclerView) view.findViewById(R.id.rvProcedure);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
mAdapter = new ProcedureAdapter(context,procedure,this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(mAdapter);
actionModeCallback = new ActionModeCallback();
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.title_lista_procedure) + " " + mCheckClass);
toolbar.setNavigationIcon(R.drawable.ic_menu_navigator);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
DrawerLayout drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else {
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab_new);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
args.putString(ARG_PARAM_TIPOLOGIA,mParamTipologia); //Passa la procedura selezionata se siamo in modifica
args.putString(ARG_PARAM_PROCEDURA,new Gson().toJson(procedure.get(lastPosition)));
if(actionMode != null) //Leva la selezione se è presente, prima di passare ad un'altro fragment.
enableActionMode(lastPosition);
try {
if ((getActivity()) != null) {
FragmentManager fragmentManager = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
Class fragmentClass = ProceduraFragment.class;
Fragment fragment = (Fragment) fragmentClass.newInstance();
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
}
} catch (java.lang.InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
});
//TODO: Implements here the load logic for procedure.
if(mParamProcedura != null && !(procedure.size() > 0)) {
procedure.add(mParamProcedura);
} else if(!(procedure.size() > 0)) {
refreshList(AlphaMAS.getData());
}
return view;
}
答案 0 :(得分:0)
解决方案
将背景设置为两个xml文件的父布局。
android:background="@color/background_color_which_you_want"
编辑1:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.procedura_menu_list, menu);
return super.onCreateOptionsMenu(menu);
}
答案 1 :(得分:0)
为什么每次都要创建一个新的片段实例?尝试这样的事情:
if (fragment == null)
fragment = (Fragment) aclass.newInstance();
答案 2 :(得分:0)
在两个Fragment
中的布局内部都使用此:
android:clickable="true"
android:focusable="true"