在 Android 上触摸导航抽屉中的选项时出现微调器

Make a spinner appears when touching an option from the Navegation Drawer on Android

我正在使用 MaterialDrawer 开发我的 Mike Penz。 当我从我的应用程序上的导航抽屉中触摸一个选项时,我试图让一个微调器出现。

我希望在单击导航抽屉上的“睡眠”选项时,数字选项出现在微调器上。像这样:

这是我的导航抽屉代码:

private void carregarMenuLateral(Bundle savedInstanceState){
    menuLateral = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(mToolBar)
            .withActionBarDrawerToggleAnimated(true)
            .withSavedInstance(savedInstanceState)
            .withSelectedItem(0)
            .withDisplayBelowToolbar(true)
            .addDrawerItems(
                    new PrimaryDrawerItem().withName("Home").withIcon(android.R.drawable.ic_btn_speak_now),
                    new PrimaryDrawerItem().withName("Peça sua música").withIcon(android.R.drawable.arrow_up_float),
                    new PrimaryDrawerItem().withName("Contato").withIcon(android.R.drawable.ic_dialog_map),
                    new PrimaryDrawerItem().withName("Sleep").withIcon(android.R.drawable.ic_dialog_email),
                    new SecondaryDrawerItem().withName("15 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("20 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("30 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("45 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("60 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("90 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("120 min").withCheckable(false),
                    new SecondaryDrawerItem().withName("cancelar").withCheckable(false)

            )
            .withDisplayBelowStatusBar(true)
            .build();

menuLateral.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
    @Override
    public boolean onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem iDrawerItem) {
        switch (position) {
            case 0: //home
                Log.d("Apertou um menu", "HOME");
                break;
            case 1: //peca sua musica
                Log.d("Apertou um menu", "MUSICA");

                break;
            case 2: //contato
                Log.d("Apertou um menu", "CONTATO");

                break;
            case 3://sleep
                Log.d("Apertou um menu", "SLEEP");
                chamaMenuSleep();
                break;
            case 4://15min
                Log.d("Apertou um menu", "15MIN");
                sleepTime = 15 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 5://20min
                Log.d("Apertou um menu", "20MIN");
                sleepTime = 20 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 6://30min
                Log.d("Apertou um menu", "30MIN");
                sleepTime = 30 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 7://45min
                Log.d("Apertou um menu", "45MIN");
                sleepTime = 45 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 8://60min
                Log.d("Apertou um menu", "60MIN");
                sleepTime = 60 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 9://90min
                Log.d("Apertou um menu", "90MIN");
                sleepTime = 90 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 10://20min
                Log.d("Apertou um menu", "120MIN");
                sleepTime = 120 * 60;//minutos * segundos por minuto
                sleep(sleepTime);
                break;
            case 11://cancelar
                Log.d("Apertou um menu", "Cancelar");
                break;
            default:
        }
        return false;
    }
});
}

这是我的微调器代码: --------------xml:

<Spinner
    android:id="@+id/sleep_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="start"/>

Main.java :

sleepSpinnerObj = (Spinner) findViewById(R.id.sleep_spinner); spinnerAdapter = ArrayAdapter.createFromResource(这个, R.array.sleep_array, android.R.layout.simple_spinner_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sleepSpinnerObj.setAdapter(spinnerAdapter);

如何将微调器添加到抽屉式导航栏?或者让它出现在导航抽屉上方?

提前致谢

我已经实现了一个新的 OverflowMenuDrawerItem,它现在是示例应用程序的一部分,它展示了带有 Overflow 菜单的自定义 DrawerItem。它使用 @Rachit Mishra 已经建议的 PopupMenu 并用 R.menu.* xml 填充它。

您还可以使用它并在溢出菜单中选择元素后更新 DrawerItems 文本。

或者要准确了解您的用例,只需使用正常的 PrimaryDrawerItem 并显示 PopupMenu 已完成 here。如果用户随后在溢出菜单中选择了一个项目,只需使用新选择的文本更新 DrawerItem


OverflowMenuDrawerItem

public class OverflowMenuDrawerItem extends BasePrimaryDrawerItem<OverflowMenuDrawerItem> {
    private int mMenu;

    public OverflowMenuDrawerItem withMenu(int menu) {
        this.mMenu = menu;
        return this;
    }

    public int getMenu() {
        return mMenu;
    }

    private PopupMenu.OnMenuItemClickListener mOnMenuItemClickListener;

    public OverflowMenuDrawerItem withOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener onMenuItemClickListener) {
        this.mOnMenuItemClickListener = onMenuItemClickListener;
        return this;
    }

    public PopupMenu.OnMenuItemClickListener getOnMenuItemClickListener() {
        return mOnMenuItemClickListener;
    }

    private PopupMenu.OnDismissListener mOnDismissListener;


    public OverflowMenuDrawerItem withOnDismissListener(PopupMenu.OnDismissListener onDismissListener) {
        this.mOnDismissListener = onDismissListener;
        return this;
    }

    public PopupMenu.OnDismissListener getOnDismissListener() {
        return mOnDismissListener;
    }

    @Override
    public String getType() {
        return "PRIMARY_OVERFLOW_MENU_ITEM";
    }

    @Override
    @LayoutRes
    public int getLayoutRes() {
        return R.layout.material_drawer_item_overflow_menu_primary;
    }

    @Override
    public void bindView(RecyclerView.ViewHolder holder) {
        Context ctx = holder.itemView.getContext();

        //get our viewHolder
        ViewHolder viewHolder = (ViewHolder) holder;

        //bind the basic view parts
        bindViewHelper((BaseViewHolder) holder);

        //handle menu click
        viewHolder.menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popup = new PopupMenu(view.getContext(), view);
                MenuInflater inflater = popup.getMenuInflater();
                inflater.inflate(mMenu, popup.getMenu());

                popup.setOnMenuItemClickListener(mOnMenuItemClickListener);
                popup.setOnDismissListener(mOnDismissListener);

                popup.show();
            }
        });

        //handle image
        viewHolder.menu.setImageDrawable(new IconicsDrawable(ctx, GoogleMaterial.Icon.gmd_more_vert).sizeDp(12).color(getIconColor(ctx)));

        //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
        onPostBindView(this, holder.itemView);
    }

    @Override
    public ViewHolderFactory getFactory() {
        return new ItemFactory();
    }

    public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
        public ViewHolder factory(View v) {
            return new ViewHolder(v);
        }
    }

    private static class ViewHolder extends BaseViewHolder {
        //protected ImageButton ibOverflow;
        private ImageButton menu;

        public ViewHolder(View view) {
            super(view);
            this.menu = (ImageButton) view.findViewById(R.id.material_drawer_menu_overflow);
        }
    }
}

layout used for this OverflowDrawerItem

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/material_drawer_item_primary"
    android:orientation="horizontal"
    android:paddingEnd="@dimen/material_drawer_vertical_padding"
    android:paddingLeft="@dimen/material_drawer_vertical_padding"
    android:paddingRight="@dimen/material_drawer_vertical_padding"
    android:paddingStart="@dimen/material_drawer_vertical_padding">

    <ImageView
        android:id="@+id/material_drawer_icon"
        android:layout_width="@dimen/material_drawer_item_primary_icon"
        android:layout_height="@dimen/material_drawer_item_primary"
        android:layout_gravity="center_vertical"
        android:paddingBottom="@dimen/material_drawer_item_primary_icon_padding"
        android:paddingEnd="@dimen/material_drawer_item_primary_icon_padding_right"
        android:paddingLeft="0dp"
        android:paddingRight="@dimen/material_drawer_item_primary_icon_padding_right"
        android:paddingStart="0dp"
        android:paddingTop="@dimen/material_drawer_item_primary_icon_padding" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical|start"
        android:orientation="vertical">

        <TextView
            android:id="@+id/material_drawer_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-medium"
            android:gravity="center_vertical|start"
            android:lines="1"
            android:singleLine="true"
            android:textDirection="anyRtl"
            android:textSize="@dimen/material_drawer_item_primary_text"
            tools:text="Some drawer text" />

        <TextView
            android:id="@+id/material_drawer_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:gravity="center_vertical|start"
            android:lines="1"
            android:singleLine="true"
            android:textDirection="anyRtl"
            android:textSize="@dimen/material_drawer_item_primary_description"
            tools:text="Some drawer text" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/material_drawer_menu_overflow"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:paddingEnd="0dp"
        android:paddingLeft="@dimen/material_drawer_vertical_padding"
        android:paddingRight="0dp"
        android:paddingStart="@dimen/material_drawer_vertical_padding"
        android:scaleType="fitCenter" />
</LinearLayout>