2016-01-20 42 views
8

我以代码方式更改语言环境,并且所有内容都正常。有一个例外,NavigationView菜单,语言没有改变。如何刷新它而不重新创建所有活动。更改语言环境后刷新NavigationView菜单

Mainactivity代码

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    context = this; 


    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    navigationView.setEnabled(false); 

    DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 
} 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" android:layout_height="match_parent" 
     android:fitsSystemWindows="true" tools:openDrawer="start"> 

      <include layout="@layout/app_bar_main" android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

     <android.support.design.widget.NavigationView android:id="@+id/nav_view" 
      android:layout_width="wrap_content" android:layout_height="match_parent" 
      android:layout_gravity="start" android:fitsSystemWindows="true" 
      app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> 

    </android.support.v4.widget.DrawerLayout> 

片段代码选择区域后

LocaleHelper.setLocale(getActivity(), getResources().getStringArray(R.array.locale)[locale]); 

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.detach(this).attach(this).commit(); 

<group android:checkableBehavior="single"> 
    <item android:id="@+id/nav_contacts" android:icon="@drawable/ic_people_black_24dp" 
     android:title="@string/contacts" android:checked="true"/> 
    <item android:id="@+id/nav_invoices" android:icon="@drawable/ic_swap_horiz_black_24dp" 
     android:title="@string/invoices" /> 
    <item android:id="@+id/nav_payments" android:icon="@drawable/ic_payment_black_24dp" 
     android:title="@string/payments" /> 
    <item android:id="@+id/nav_history" android:icon="@drawable/ic_receipt_black_24dp" 
     android:title="@string/history" /> 
    <item android:id="@+id/nav_settings" android:icon="@drawable/ic_settings_black_24dp" 
     android:title="@string/settings" /> 
    <item android:id="@+id/nav_info" 
       android:title="@string/info" android:icon="@android:drawable/ic_dialog_info"/> 
</group> 

感谢名单

+0

您是如何填充导航器抽屉的?列表或简单的查看项目。 – theJango

+0

菜单布局中的条目 – koa73

回答

2

我决定这个问题

private void refreshNavigationView(){ 

    getActivity().setContentView(R.layout.activity_main); 
    NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view); 

} 

这种变化区域

后是足够多的刷新NavigationView但我输在Mainactivity

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    // Checks the locale has changed 
    if (!currentLocale.equals(newConfig.locale)) { 
     currentLocale = newConfig.locale; 

     this.setContentView(R.layout.activity_main); 
     NavigationView navigationView = (NavigationView) this.findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(MainActivity.this); 
    } 
} 
onNavigationItemSelected

结果

在片段

getActivity().onConfigurationChanged(getActivity().getResources().getConfiguration()); 
0

你可以试试下面的代码。我没有测试过,但我希望它能起作用。

for (int i = 0, count = navigationView.getChildCount(); i < count; i++) { 
    final View child = navigationView.getChildAt(i); 
    if (child != null && child instanceof ListView) { 
     final ListView menuView = (ListView) child; 
     final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter(); 
     final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter(); 
     wrapped.notifyDataSetChanged(); 
    } 
} 
+0

无法正常工作 – koa73

+0

wrapped.getCount()返回6它真的,我heve菜单上的6个项目,但notifyDataSetChanged()不刷新菜单或不了解新的语言环境 – koa73

+0

我已经这样做了: (navigationView.getMenu())addSubMenu( “XXX”);它提供了一个新的项目,但没有改变区域设置 – koa73