2016-01-20 6 views
8

Я меняю язык программным путем из фрагмента, и все в порядке. Существует одно исключение - меню NavigationView, где язык не меняется. Как обновить его, не обновляя всю активность.Обновить NavigationView меню после изменения locale

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> 

Thanx

+0

Как вы заполняете свой навигационный ящик? список или простые элементы просмотра. – theJango

+0

элементов из меню макета – koa73

ответ

2

Я решил эту проблему

private void refreshNavigationView(){ 

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

} 

Это прийти для обновления NavigationView после изменения локали

Но я потеряю onNavigationItemSelected

В результате на 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); 
    } 
} 

У фрагмента

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() return 6 it'l true, i heve 6 элементов в меню, но notifyDataSetChanged() не обновлять меню или не понимать новый язык – koa73

+0

Я сделал это: . (navigationView.getMenu()) addSubMenu ("ххх"); Он применил новый элемент, но не изменил локаль. – koa73