如何在android应用程序的导航抽屉菜单中放置缩进

时间:2016-01-25 12:37:15

标签: android google-play navigation-drawer material-design

我想像我在图片中看到的那样创建Google Play商店应用程序菜单。

我现在面临的难点是在菜单中放置缩进空间以在应用程序中创建第二级菜单。

enter image description here

这是我现在正在使用的代码。

  

acivity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camara"
            android:icon="@android:drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@android:drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@android:drawable/ic_menu_manage"
            android:title="Tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@android:drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@android:drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>
  

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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>
  

MainActivity.java

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);

        //How to change elements in the header programatically
        View headerView = navigationView.getHeaderView(0);
        TextView emailText = (TextView) headerView.findViewById(R.id.email);
        emailText.setText("newemail@email.com");

        navigationView.setNavigationItemSelectedListener(this);

2 个答案:

答案 0 :(得分:1)

以下是我菜单中的RecyclerView适配器示例。 这里我有两种项目类型 - 选择和常规。你可能会有4种类型   - 常规项目   - 定期选择   - 子项目   - 子选择

 public class MenuItemAdapter extends RecyclerView.Adapter<MenuItemHolder> {

        private static final int VIEW_ITEM_SELECTED = 0;
        private static final int VIEW_ITEM_UNSELECTED = 1;

        private static final int MESSAGES_POSITION = 0;
        private static final int HOME_POSITION = 1;
        private static final int SEARCH_POSITION = 2;
        private static final int FAVORITES_POSITION = 3;


        private final ItemHolder.OnItemClickListener onItemClickListener;

        public final int[] MENU_TEXTS = {
                R.string.messages_menu_item_title,
                R.string.home_menu_item_title,
                R.string.search_menu_item_title,
                R.string.favorites_menu_item_title,

        };

        private static ArrayList<Integer> MENU_IMAGES;

        private int selectedPosition = 1;

        public MenuItemAdapter(ItemHolder.OnItemClickListener listener) {

            this.onItemClickListener = listener;
            MENU_IMAGES = new ArrayList<>();

            MENU_IMAGES.add(R.drawable.messages);
            MENU_IMAGES.add(R.drawable.home_yellow);
            MENU_IMAGES.add(R.drawable.search);
            MENU_IMAGES.add(R.drawable.favorites);

        }

        // Create new views (invoked by the layout manager)
        @Override
        public MenuItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // create a new view
            View v = null;
            switch (viewType){
                case VIEW_ITEM_SELECTED :
                   v = LayoutInflater.from
                           (parent.getContext()).inflate(R.layout.menu_item_selected, parent, false);
                    break;
                case VIEW_ITEM_UNSELECTED :
                    v = LayoutInflater.from
                            (parent.getContext()).inflate(R.layout.menu_item_unselected, parent, false);
            }

            return new MenuItemHolder(v, (position, view) -> {
                onItemClickListener.onItemClick(position, view);
                selectedPosition = position;
                MenuItemAdapter.this.notifyDataSetChanged();
            });
        }

        @Override
        public void onBindViewHolder(MenuItemHolder holder, int position) {
            holder.title.setText(MENU_TEXTS[position]);
            holder.image.setImageResource(MENU_IMAGES.get(position));
        }

        @Override
        public int getItemCount() {
            return MENU_TEXTS.length;
        }

        @Override
        public int getItemViewType(int position){
            if (position == selectedPosition){
                return VIEW_ITEM_SELECTED;
            }
            return VIEW_ITEM_UNSELECTED;
        }

        public void setHomePosition(){
            selectedPosition = HOME_POSITION
            notifyItemChanged(selectedPosition);
        }

        ...

答案 1 :(得分:0)

您可以将片段作为菜单,而不是使用新的NavigationView,它简单易用,易于使用并保留设计指南。 在那个Fragment中放置一个带有2个ViewTypes的RecyclerView:1表示缩进项目,1表示常规不缩进。