【问题标题】:Android NavigationView displays under NavigationBar, and cannot be clickedAndroid NavigationView 显示在 NavigationBar 下,无法点击
【发布时间】:2016-01-14 15:09:22
【问题描述】:

如您所见,NavigationView的背景已经设置为android:clipToPadding='false',在透明NavigationBar下可以看到。

但是NavigationView(NavigationDrawer)不能完全滚动到NavigationBar上方,如何解决?

我希望列表项滚动到NavigationBar 的顶部,这意味着最后一个Send 不在NavigationBar 之下而是之上。


layout/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"
        android:clipToPadding="false"
        android:clipChildren="false"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

好吧,代码是这样运行的,我找不到有用的答案。

欢迎任何想法,谢谢!

【问题讨论】:

  • @onurtaskin 谢谢,按码计算确实是一个可行的方案,但是当用户隐藏/显示NavigationBar时出现问题。它不能随着NavigationBar 的隐藏/显示一起改变,因为我没有找到捕获NavigationBar 事件(TT)的解决方案所以我更喜欢获得改变xml 文件的解决方案:)跨度>
  • onur taskin上面的链接和我的问题不一样,所以不能用。

标签: android material-design navigationbar navigationview


【解决方案1】:

感谢mehrdad khosravi!他的解决方案真的是一个很好的方向,也更有艺术性。

但是我太懒了,所以我有另一个但不是那么艺术的解决方案:扩展自NavigationView

package your.package;

import android.content.Context;
import android.support.design.widget.NavigationView;
import android.util.AttributeSet;

import another.package.ScreenTool;

/**
 * This view makes the NavigationDrawer fit system view.
 * Created by MewX on 1/19/2016.
 */
public class NavigationFitSystemView extends NavigationView {
    public NavigationFitSystemView(Context context) {
        super(context);
    }

    public NavigationFitSystemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NavigationFitSystemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {

        // TODO: optimize for tablet screen layout
        super.onMeasure(widthSpec, heightSpec- ScreenTool.getCurrentNavigationBarSize(getContext()).y);
    }
}

还有,ScreenTool.java

package another.package;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;

/**
 * This class contains many screen-relate functions.
 * Created by MewX on 1/18/2016.
 */
@SuppressWarnings("unused")
public class ScreenTool {
    /**
     * get status bar height in px, this is the defined value whenever statusBar is displayed.
     * @param context current context
     * @return px int status bar height
     */
    public static int getStatusBarHeightValue(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result; // in px
    }

    /**
     * get navigation bar height in px, this is the defined value whenever navBar is displayed.
     * @param context current context
     * @return px int navigation bar height
     */
    public static int getNavigationBarHeightValue(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result; // in px
    }

    /**
     * get current navigation bar size:
     *   if bar not displaying, the values is zero;
     *   else is the width and height value;
     * @param context current context
     * @return new Point(width, height)
     */
    public static Point getCurrentNavigationBarSize(Context context) {
        Point appUsableSize = getAppUsableScreenSize(context);
        Point realScreenSize = getRealScreenSize(context);

        // navigation bar on the right
        if (appUsableSize.x < realScreenSize.x) {
            return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
        }

        // navigation bar at the bottom
        if (appUsableSize.y < realScreenSize.y) {
            return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
        }

        // navigation bar is not present
        return new Point();
    }

    /**
     * get application usable screen size.
     * @param context current size
     * @return new Point(width, height)
     */
    public static Point getAppUsableScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size;
    }

    /**
     * get actual/real screen size, this is the full screen size.
     * @param context current context
     * @return new Point(width, height)
     */
    public static Point getRealScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();

        if (Build.VERSION.SDK_INT >= 17) {
            display.getRealSize(size);
        } else if (Build.VERSION.SDK_INT >= 14) {
            try {
                size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
                size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
            } catch (Exception e) {
                size = new Point(0, 0);
            }
        }
        return size;
    }
}

然后,将布局文件从&lt;android.support.design.widget.NavigationView /&gt;更改为&lt;your.package.NavigationFitSystemView /&gt;


最后,更改您在其他来源中的引用,例如 MainActivity.java

最后,屏幕看起来像这样,并且可以在 NavigationBar 隐藏或显示时动态更改。

【讨论】:

    【解决方案2】:

    删除

    name="android:windowTranslucentNavigation">true
    

    来自style.xml中的主题

    【讨论】:

    【解决方案3】:

    您可以在导航中使用 RecyclerView

    在您的导航适配器中:

    public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.ViewHolder>
    

    简单的鳕鱼:

    navigation_drawer_separator.xml

    <?xml version="1.0" encoding="utf-8"?>
    <View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:background="@color/black_slowest"/>
    

    partial_navigation_drawer.xml

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="your package name.Fragment.NavigationDrawerFragment"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer"/>
    

    navigation_drawer_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center_vertical|left"
                  android:orientation="horizontal"
                  android:padding="12dp">
    
        <TextView
            android:id="@+id/rowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    
        <ImageView
            android:id="@+id/rowIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
    

    navigation_drawer_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="178dp"
                  android:background="@color/red_dirty"
                  android:gravity="center_vertical|start"
                  android:orientation="vertical"
                  android:paddingEnd="18dp"
                  android:paddingRight="18dp">
    
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/circleView"
            android:layout_width="70dp"
            android:layout_height="70dp"/>
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="12dp"
            android:text="mehrdad"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            />
    
        <TextView
            android:id="@+id/txt_edit_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="8dp"
            android:text="mehrdadkhosravi"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="normal"
            />
    
    </LinearLayout>
    

    fragment_navigation_drawer.xml

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RecyclerView"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:background="@color/light_gray"
    />
    

    菜单/navigation_drawer_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <group android:id="@+id/group_special"  android:checkableBehavior="single">
    
            <item
                android:id="@+id/nav_item_what_the_cook"
                android:checked="false"
                android:icon="@mipmap/ic_launcher"
                android:title="main"/>
    
            <item
                android:id="@+id/nav_item_ghablameh_suggest"
                android:checked="false"
                android:icon="@mipmap/ic_launcher"
                android:title="something1"/>
        </group>
        <group android:id="@+id/group_packs" android:checkableBehavior="single">
            <item
                android:id="@+id/nav_item_favorites"
                android:checked="false"
                android:icon="@mipmap/ic_launcher"
                android:title="something2"/>
            <item
                android:id="@+id/nav_item_favorite_tags"
                android:checked="false"
                android:icon="@mipmap/ic_launcher"
                android:title="something3"/>
            <item
                android:id="@+id/nav_item_week_bests"
                android:checked="false"
                android:icon="@mipmap/ic_launcher"
                android:title="aboute us"/>
        </group>
    </menu>
    

    NavItemType.java

    package your package name.Enum;
    
    /**
     * Created by mehrdad on 11/16/2015.
     */
    public enum NavItemType
    {
        Item,
        Group
    }
    

    NavMenuItem.java

    package your package name.Model;
    
    import your package name.Enum.NavItemType;
    
    /**
     * Created by mehrdad on 11/16/2015.
     */
    public class NavMenuItem
    {
        private NavItemType itemType;
        private int resourceId;
    
        public NavItemType getItemType()
        {
            return itemType;
        }
    
        public void setItemType(NavItemType itemType)
        {
            this.itemType = itemType;
        }
    
        public int getResourceId()
        {
            return resourceId;
        }
    
        public void setResourceId(int resourceId)
        {
            this.resourceId = resourceId;
        }
    }
    

    NavMenuParser.java

    package your package name.Util;
    
    import android.support.v7.view.menu.MenuBuilder;
    import android.view.MenuItem;
    
    import your package name.Enum.NavItemType;
    import your package name.Model.NavMenuItem;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by mehrdad on 11/16/2015.
     */
    public class NavMenuParser
    {
        public static List<NavMenuItem> parse(MenuBuilder menu){
            List<NavMenuItem> navMenuItems = new ArrayList<>();
            int groupId = menu.getItem(0).getGroupId();
            for (MenuItem menuItem : menu.getVisibleItems())
            {
                if(menuItem.getGroupId() != groupId)
                {
                    groupId = menuItem.getGroupId();
                    NavMenuItem navGroup = new NavMenuItem();
                    navGroup.setItemType(NavItemType.Group);
                    navGroup.setResourceId(groupId);
                    navMenuItems.add(navGroup);
                }
                NavMenuItem navItem = new NavMenuItem();
                navItem.setItemType(NavItemType.Item);
                navItem.setResourceId(menuItem.getItemId());
                navMenuItems.add(navItem);
            }
            return navMenuItems;
        }
    }
    

    NavigationDrawerFragment.java

    package com.khosravi.mehrdadz.garagesale.Fragment;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v7.view.menu.MenuBuilder;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.MenuInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import your package name.Adapter.NavigationDrawerAdapter;
    import your package name.R;
    import your package name.Util.SessionManager;
    
    import de.hdodenhof.circleimageview.CircleImageView;
    
    
    public class NavigationDrawerFragment extends Fragment
    {
    
        RecyclerView mRecyclerView;                           // Declaring RecyclerView
        NavigationDrawerAdapter mAdapter;                        // Declaring Adapter For Recycler View
        RecyclerView.LayoutManager mLayoutManager;            // Declaring Layout Manager as a linear layout manager
        private SessionManager session;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View fragmentView = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    
            session = new SessionManager(getActivity());
    
            String userName = session.getUsername();
            String email = session.getEmail();
            String pictureUrl = session.getPictureUrl();
    
            mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
    
            mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size
            MenuBuilder menu = new MenuBuilder(getActivity());
            new MenuInflater(getActivity()).inflate(R.menu.navigation_drawer_menu, menu);
            mAdapter = new NavigationDrawerAdapter(getActivity(),menu, userName, email, pictureUrl );       // Creating the Adapter of NavigationDrawerAdapter class(which we are going to see in a bit)
            // And passing the titles,icons,navigation_drawer_header view name, navigation_drawer_header view email,
            // and navigation_drawer_header view profile picture
    
            mRecyclerView.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView
    
            mLayoutManager = new LinearLayoutManager(getActivity());                 // Creating a layout Manager
    
            mRecyclerView.setLayoutManager(mLayoutManager);                 // Setting the layout Manager
    
            return fragmentView;
        }
    }
    

    NavigationDrawerAdapter.java

    package your package name.Adapter;
    
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.support.v7.view.menu.MenuBuilder;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.android.volley.toolbox.ImageLoader;
    import com.android.volley.toolbox.NetworkImageView;
    import your package name.Activity.LoginActivity;
    import your package name.Activity.RegisterActivity;
    import your package name.Enum.NavItemType;
    import your package name.Model.NavMenuItem;
    import your package name.Network.VolleySingleton;
    import your package name.R;
    import your package name.Util.AppConstant;
    import your package name.Util.NavMenuParser;
    
    import java.util.List;
    
    
    public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.ViewHolder>
    {
    
        private static final int TYPE_HEADER = 0;  // Declaring Variable to Understand which View is being worked on
        // IF the view under inflation and population is navigation_drawer_header or Item
        private static final int TYPE_ITEM = 1;
        private static final int TYPE_SEPARATOR = 2;
        private String username;        //String Resource for navigation_drawer_header View txtUsername
        private List<NavMenuItem> navMenuItems;
        private Menu menu;
        private String userPictureUrl;        //int Resource for navigation_drawer_header view userPictureUrl picture
        private String email;       //String Resource for navigation_drawer_header view txtEmail
        private ImageLoader imageLoader;
        // Creating a ViewHolder which extends the RecyclerView View Holder
        // ViewHolder are used to to store the inflated views in order to recycle them
    
        public NavigationDrawerAdapter(final Context context,MenuBuilder menu, final String username, String email, String userPictureUrl)
        { // NavigationDrawerAdapter Constructor with titles and icons parameter
            navMenuItems = NavMenuParser.parse(menu);
            VolleySingleton volleySingleton = VolleySingleton.getInstance(context);
            imageLoader = volleySingleton.getImageLoader();
            this.menu = menu;
            this.username = username;
            this.email = email;
            this.userPictureUrl = userPictureUrl;                     //here we assign those passed values to the values we declared here in adapter
            ViewHolder.setViewHolderClickListener(new ViewHolder.ViewHolderClickListener()
            {
                @Override
                public void onNavigationItemClick(int position)
                {
    
                }
    
                @Override
                public void onNavigationHeaderClick()
                {
                    Toast.makeText(context, "Go to imgProfilePicture info!", Toast.LENGTH_LONG).show();
    
                    new AlertDialog.Builder(context).setIcon(android.R.drawable.ic_dialog_info).setTitle("welcome")
                            .setMessage("...")
                            .setPositiveButton("login", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    Intent intent = new Intent(context,LoginActivity.class);
                                    context.startActivity(intent);
    //                                startActivity(intent);
                                }
                            }).setNegativeButton("register", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Intent intent = new Intent(context, RegisterActivity.class);
                            context.startActivity(intent);
    //                        startActivity(intent);
                        }
                    }).show();
    
    //                Intent i = new Intent(context, UserDetailActivity.class);
    //                Bundle bundle = new Bundle();
    //                bundle.putString("username", username);
    //                i.putExtras(bundle);
    //                context.startActivity(i);
                }
            });
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View v = null;
            switch (viewType)
            {
                case TYPE_HEADER:
                    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_header, parent, false); //Inflating the layout
                    break;
                case TYPE_ITEM:
                    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_item, parent, false); //Inflating the layout
                    break;
                case TYPE_SEPARATOR:
                    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_separator, parent, false); //Inflating the layout
                    break;
            }
            return new ViewHolder(v, viewType); // Returning the created object
    
        }
    
    
        //Below first we override the method onCreateViewHolder which is called when the ViewHolder is
        //Created, In this method we inflate the navigation_drawer_item layout if the viewType is Type_ITEM or else we inflate navigation_drawer_header.xml_drawer_header.xml
        // if the viewType is TYPE_HEADER
        // and pass it to the view holder
    
        //Next we override a method which is called when the item in a row is needed to be displayed, here the int position
        // Tells us item at which position is being constructed to be displayed and the holder id of the holder object tell us
        // which view type is being created 1 for item row
        @Override
        public void onBindViewHolder(ViewHolder holder, int position)
        {
            switch (holder.viewType)
            {
                case TYPE_ITEM:
                    // as the list view is going to be called after the navigation_drawer_header view so we decrement the
                    // position by 1 and pass it to the holder while setting the text and image
                    MenuItem menuItem = menu.findItem(navMenuItems.get(position - 1).getResourceId());
                    holder.txtNavItemText.setText(menuItem.getTitle()); // Setting the Text with the array of our Titles
                    holder.imgNavItemIcon.setImageDrawable(menuItem.getIcon());// Setting the image with array of our icons
                    break;
                case TYPE_HEADER:
                    if(userPictureUrl != null)
                    {
                        String pictureUrl = AppConstant.URL_ROOT + userPictureUrl;
                        holder.imgProfilePicture.setImageUrl(pictureUrl,imageLoader);           // Similarly we set the resources for navigation_drawer_header view
                    }
                    holder.txtUsername.setText(username);
                    holder.txtEmail.setText(email);
                    break;
            }
        }
    
        // This method returns the number of items present in the list
        @Override
        public int getItemCount()
        {
            return navMenuItems.size() + 1; // the number of items in the list will be +1 the titles including the navigation_drawer_header view.
        }
    
        // With the following method we check what type of view is being passed
        @Override
        public int getItemViewType(int position)
        {
            if (position == 0)
                return TYPE_HEADER;
            if (navMenuItems.get(position - 1).getItemType() == NavItemType.Group)
                return TYPE_SEPARATOR;
    
            return TYPE_ITEM;
        }
    
    
        public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
        {
            private int viewType;
    
            private TextView txtNavItemText;
            private ImageView imgNavItemIcon;
            private NetworkImageView imgProfilePicture;
            private TextView txtUsername;
            private TextView txtEmail;
            private static ViewHolderClickListener viewHolderClickListener;
    
            public ViewHolder(View itemView, int viewType)
            {
                // Creating ViewHolder Constructor with View and viewType As a parameter
                super(itemView);
                this.viewType = viewType;
    
                if (viewType != TYPE_SEPARATOR)
                {
                    itemView.setClickable(true);
                    itemView.setOnClickListener(this);
                    // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
                    switch (viewType)
                    {
                        case TYPE_ITEM:
                            txtNavItemText = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of txtNavItemText from navigation_drawer_item_rowon_drawer_item_row.xml
                            imgNavItemIcon = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from navigation_drawer_item_rowon_drawer_item_row.xml                 // setting holder id as 1 as the object being populated are of type item row
                            break;
                        case TYPE_HEADER:
                            txtUsername = (TextView) itemView.findViewById(R.id.name);         // Creating Text View object from navigation_drawer_headertion_drawer_header.xml for name
                            txtEmail = (TextView) itemView.findViewById(R.id.txt_edit_email);       // Creating Text View object from navigation_drawer_header.xml_drawer_header.xml for txtEmail
                            imgProfilePicture = (NetworkImageView) itemView.findViewById(R.id.circleView);// Creating Image view object from navigation_drawer_headertion_drawer_header.xml for userPictureUrl pic                 // Setting holder id = 0 as the object being populated are of type navigation_drawer_header view
                            break;
                    }
                }
    
            }
    
            public static void setViewHolderClickListener(ViewHolderClickListener viewHolderClickListener)
            {
                ViewHolder.viewHolderClickListener = viewHolderClickListener;
            }
    
            public interface ViewHolderClickListener
            {
                void onNavigationItemClick(int position);
    
                void onNavigationHeaderClick();
            }
    
            @Override
            public void onClick(View v)
            {
                switch (viewType)
                {
                    case TYPE_HEADER:
                        viewHolderClickListener.onNavigationHeaderClick();
                        break;
    
                    case TYPE_ITEM:
                        viewHolderClickListener.onNavigationItemClick(getAdapterPosition());
                        break;
                }
            }
        }
    }
    

    在你的家庭活动中

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activity.HomeActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <include layout="@layout/partial_toolbar"/>
    
    
        home activity budy 
    
    
        </LinearLayout>
    
        <include layout="@layout/partial_navigation_drawer"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    和你的家庭活动java端

    DrawerLayout drawerLayout;
    drawerLayout =(DrawerLayout) findViewById(R.id.drawer);
    
    @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch (item.getItemId())
            {
                case R.id.toggle_drawer:
                    drawerLayout.openDrawer(GravityCompat.START);
                break;
            }
            return true;
        }
    

    【讨论】:

    • 您的意思是使用RecyclerView 来保存NavigationView 中的所有项目?我想知道我可能需要一一处理所有项目的颜色,就像this function? (因为每次点击后,item都会改变颜色。)无论如何,请在愉快的时候写更多的细节,谢谢~
    • 是的。并且它的工作良好并且完全滚动,但这是一条艰难的道路。我写了 6 个 xml 类和 3 个 java 类来使用它。我不忙的时候会给你我的代码
    • 嗨,您还需要那个答案吗?
    • 我没有时间为您编辑此代码。但你可以使用它,如果你可以编辑它
    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多