我有一个导航抽屉实现,但问题是当按下后退按钮时,绘图中的已检查项目与当前片段(仅通过导航抽屉实际添加的最后一个片段)不对应
这里有一个解决方案:Updating the Navigation Drawer (with DrawerLayout) when back button is pressed
解决方案陈述如下:
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager fm = getSupportFragmentManager();
String stackName = null;
for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
stackName = fm.getBackStackEntryAt(entry).getName();
Log.i("BC", "stackEntry" + entry);
}
if (stackName == "Fragment1"){
mDrawerList.setItemChecked(0, true);
} else if (stackName == "Fragment2") {
mDrawerList.setItemChecked(1, true);
}
});
问题是有条件的。当您拥有大量物品时,必须手动更改要检查的导航项目的索引是一件痛苦的事情
我想要做的是监听Backstack中的更改,如果检测到,则获取当前片段的id(如布局文件中所定义)并将其传递给navigationView.setCheckedItem()
以自动检查相应的导航项目。像这样:
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(R.id.nav_new);
在我的主要活动课程中,如何以R.id.xxx
的形式获取当前片段的ID,以便将其传递给setCheckedItem
?
答案 0 :(得分:0)
你可以这样做
添加片段时执行此操作
RewriteEngine On
RewriteRule ^(home|empresa|instalaciones|acceso_clientes|nuevo_cliente|enviar_cliente|presupuestos|enviar_presupuesto|subir_archivo|enviar_archivo|suscribete|enviar_suscripcion|trabajos|hp-indigo-digital|packaging_plus|equipo_humano|oferta|enviar_oferta|contacto|privacidad)$ $1.php [L]
RewriteRule ^trabajos/([0-9]+) trabajos.php?id=$1 [L]
RewriteRule ^seccion_empresa/(.*) seccion_empresa.php?seccion=$1 [L]
然后
FragmentTransaction.addToBackStack(""+R.id.nav_new); // Use the id of navigation item which was clicked.
注意:当getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager fm = getSupportFragmentManager();
String stackName = null;
for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
stackName = fm.getBackStackEntryAt(entry).getName();
Log.i("BC", "stackEntry" + entry);
}
int navItemId = -1;
try{
navItemId = Integer.parseInt(stackName);
} catch(NumberFormatException nfe) {
}
if(navItemId > 0){
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(navItemId);
}
else
Log.e("", "Couldn't find the id");
});
addToBackStack
时,getName()
中的任何内容都会返回给您
答案 1 :(得分:0)
you can handle it manually.
Make a function in base class to highlight desired item like
mDrawerList.setItemChecked(INDEX NUMBER, true);
Trick is make a model class for side menu items in which INDEX NUMBER is saved.
When you replace new fragment call that function and store currentFragment a well.
and when you press back button ovverides
public boolean onKeyDown(int keyCode, KeyEvent event) {}
and call below function
public void popAFragment(){
FragmentManager fragmentManager = getSupportFragmentManager();
if(fragmentManager != null){
int c = fragmentManager.getBackStackEntryCount();
if(c>0){
currentFragment = (BaseFragment) fragmentManager.getFragments().get(c-1);
fragmentManager.popBackStack();
setPosition(currentFragment.INDEX_NUMBER);
}
else {
finish();
}
}