Android – how to add spinner as a project in the navigation drawer

I want to add a spinner to my navigation drawer as a project. Where should I put the spinner? Where does the layout of the rotator expand? Where to initialize the spinner? I hope it looks like this:

This is where I add items:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group
    android:id="@+id/group1"
    android:checkableBehavior="single">
    <item
        android:id="@+id/nav_login"
        android:icon="@drawable/ic_login"
        android:title="@string/login_menu_item"/>
    <item
        android:id="@+id/nav_signup"
        android:icon="@drawable/ic_signup"
        android:title="@string/signup_menu_item"/>
    </group>

<item android:title="@string/language">
    <menu>
        <item
            android:id="@+id/nav_eng"
            android:title="@string/english">
        </item>

        <item
            android:id="@+id/nav_heb"
            android:title="@string/hebrew">
        </item>
    </menu>

This is my drawer layout:

<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:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          android:orientation="vertical">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:title="Masü"
    />

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@color/colorAccent"
        app:itemTextColor="@color/textColorSecondary"
        app:menu="@menu/activity_home_drawer"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

Click on an item, which is how it works:

public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    mDrawerLayout.closeDrawers();

    if (id == R.id.nav_login) {
        if (mIsLoggedin) {
            logout();
        } else {
            mFragmentTransaction = mFragmentManager.beginTransaction();
            mFragmentTransaction.replace(R.id.fragment_container, new LoginFragment()).commit();
        }

resolvent:

Step 1. Add items in menu.xml

 <item
            android:id="@+id/navigation_drawer_item3"
            android:icon="@android:drawable/ic_menu_share"
            android:title=""
            app:actionLayout="@layout/spinner"/> 

Step 2. Create a layout for the spinner view

<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical" />

Step 3. Set the spinner data in the active file

Spinner spinner = (Spinner) navigationView.getMenu().findItem(R.id.navigation_drawer_item3).getActionView();
    spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,language));
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
          Toast.makeText(MainActivity.this,language[position],Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Step 4. If necessary, add the Android support design library to the project

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>