Android NavigationView显示在NavigationBar下,无法单击

时间:2016-01-14 15:09:23

标签: android material-design navigationbar navigationview

正如您所看到的,$scope.usefulData = {}; $ionicModal.fromTemplateUrl('templates/mypopover.html', { scope: $scope }).then(function(modal) { $scope.modalMypopover = modal; }); $scope.closeMypopover = function() { $scope.modalMypopover.hide(); }; $scope.mypopover = function() { $scope.modalMypopover.show(); }; $scope.doMypopover = function() { console.log('Doing mypopover'); $timeout(function() { $scope.closeMypopover(); }, 1000); }; 的背景已设置NavigationView,可以在透明android:clipToPadding='false'下看到。

BackGround NavigationView

但是NavigationBar无法完全向上滚动到NavigationView(NavigationDrawer)以上,如何解决这个问题?

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

布局/ activity_main.xml中

NavigationBar

嗯,代码就像这样运行,我无法找到有用的答案。

欢迎任何想法,谢谢!

3 个答案:

答案 0 :(得分:4)

感谢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;
    }
}

然后,将布局文件从<android.support.design.widget.NavigationView />更改为<your.package.NavigationFitSystemView />

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

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

NavigationFitSystemView

答案 1 :(得分:0)

您可以在导航中使用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;
    }

答案 2 :(得分:0)

删除

name="android:windowTranslucentNavigation">true

来自style.xml

中的主题