更改区域设置后刷新NavigationView菜单

时间:2016-01-20 09:32:25

标签: android locale navigationview

我正在以编程方式从片段更改语言环境,一切正常。有一个例外,NavigationView菜单,语言没有改变。如何在不重新创建所有活动的情况下刷新它。

主要活动代码

@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>

感谢名单

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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();
    }
}