5

Ich möchte nur die Menüpunkte der Navigationsansicht scrollen, aber es auch die Kopfzeile mit ihm scrollen. Bitte jeder Körper mir sagen, wie dies zu erreichen ... Hier ist mein Code:Android Navigationsansicht: Scrollen Sie nur die Menüpunkte Ausgabe

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     .......  
     <android.support.design.widget.NavigationView 
      android:id="@+id/navigation_view" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      app:headerLayout="@layout/header" 
      app:menu="@menu/drawer" /> 
    </android.support.v4.widget.DrawerLayout> 

header.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="190dp" 
     android:background="@drawable/background_material" 
     android:orientation="vertical" 
     > 

     <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/profile_image" 
      android:layout_width="76dp" 
      android:layout_height="76dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="24dp" 
      android:layout_marginStart="24dp" 
      android:src="@mipmap/ic_launcher" 
      app:border_color="#FF000000"/
    </RelativeLayout> 

drawer_menu_items.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/inbox" 
      android:checked="false" 
      android:icon="@drawable/ic_inbox_black" 
      android:title="@string/inbox_string" /> 

     <item 
      android:id="@+id/starred" 
      android:checked="false" 
      android:icon="@drawable/ic_star_black" 
      android:title="@string/starred_string" /> 

     <item 
      android:id="@+id/sent_mail" 
      android:checked="false" 
      android:icon="@drawable/ic_send_black" 
      android:title="@string/sent_mail_string" /> 

     <item 
      android:id="@+id/drafts" 
      android:checked="false" 
      android:icon="@drawable/ic_drafts_black" 
      android:title="@string/draft_string" /> 


     <item 
      android:id="@+id/allmail" 
      android:checked="false" 
      android:icon="@drawable/ic_email_black" 
      android:title="@string/all_mail_string" /> 
     <item 
      android:id="@+id/trash" 
      android:checked="false" 
      android:icon="@drawable/ic_delete_black" 
      android:title="@string/trash_string" /> 
     <item 
      android:id="@+id/spam" 
      android:checked="false" 
      android:icon="@drawable/ic_error_black" 
      android:title="@string/spam_string" /> 
     <item 
      android:id="@+id/abc" 
      android:checked="false" 
      android:icon="@android:drawable/ic_menu_camera" 
      android:title="Camera" /> 
     <item 
      android:id="@+id/abcd" 
      android:checked="false" 
      android:icon="@android:drawable/ic_menu_call" 
      android:title="Call" /> 
     <item 
      android:id="@+id/abcde" 
      android:checked="false" 
      android:icon="@android:drawable/ic_menu_gallery" 
      android:title="Gallery" /> 

     <item 

      android:checked="false" 
      android:icon="@android:drawable/ic_menu_gallery" 
      android:title="Gallery" /> 
     <item 

      android:checked="false" 
      android:icon="@android:drawable/ic_menu_compass" 
      android:title="Compass" /> 
     <item 

      android:checked="false" 
      android:icon="@android:drawable/ic_menu_myplaces" 
      android:title="My Places" /> 

    </group> 
</menu> 

Wie kann ich nur den Menüpunkt blättern nicht der Kopf ... Ich habe auch ein paar Bilder angehängt ... Before ScrollingAfter Scrolling

+1

Sie können es derzeit nicht erreichen –

+0

Ich denke, ich sollte eine ListView innerhalb der Navigationsansicht hinzufügen, anstatt Menüelemente hinzufügen? Was sagst du darüber? @Gabriele Mariotti –

+0

In diesem Fall nicht die NavigationView verwenden. –

Antwort

4

sollten Sie in der Lage sein, Ihre Header wie so außerhalb des NavigationView, zu platzieren:

<?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" 

    <!-- nav drawer --> 
    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     > 
     <!-- header --> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="190dp" 
      android:background="@drawable/background_material" 
      android:orientation="vertical" 
      > 

      <de.hdodenhof.circleimageview.CircleImageView 
       android:id="@+id/profile_image" 
       android:layout_width="76dp" 
       android:layout_height="76dp" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:layout_marginLeft="24dp" 
       android:layout_marginStart="24dp" 
       android:src="@mipmap/ic_launcher" 
       app:border_color="#FF000000" /> 
     </RelativeLayout> 

     <!-- menu --> 
     <android.support.design.widget.NavigationView 
      android:id="@+id/navigation_view" 
      android:layout_width="wrap_content" 
      android:layout_height="0px" 
      android:layout_weight="1" 
      android:layout_gravity="start" 
      app:menu="@menu/drawer" /> 
    </FrameLayout> 
</android.support.v4.widget.DrawerLayout> 

Anmerkung: ich habe ein FrameLayout einzukapseln alles in einer einzigen Ansicht für die DrawerLayout, fügt die NavigationView Höhe automatisch angepasst, um den verfügbaren Platz unter Ihrem Kopf zu verwenden, und habe das headerLayout Attribut entfernt.

0

Ich konnte Lornes Antwort nicht zur Arbeit bekommen, aber ich hatte etwas Erfolg, indem ich ein kundenspezifisches NavigationView verwendete. Es ist sehr hacky, erreicht aber den gewünschten Effekt.

Die Methoden, die der benutzerdefinierten NavigationView hinzugefügt wurden, ermöglichen es Ihnen, eine Kopfzeile über der Navigationsansicht hinzuzufügen und das Menü dann so zu skalieren, dass es unter die Kopfzeile passt.

Kommentare für Verbesserungen sind willkommen

Die overrided NavigationView

public class CustomNavigationView extends NavigationView { 

public CustomNavigationView(Context context) { 
    super(context); 
} 


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


public CustomNavigationView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 


// Consumes touch in the NavigationView so it doesn't propagate to views below 
public boolean onTouchEvent (MotionEvent me) { 
    return true; 
} 


// Inflates header as a child of NavigationView 
public void createHeader(int res) { 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    View view = inflater.inflate(res, this, false); 

    // Consumes touch in the header so it doesn't propagate to menu items below 
    view.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event){ 
      return true; 
     } 
    }); 

    addView(view); 
} 


// Positions and sizes the menu view 
public void sizeMenu(View view) { 
    // Height of header 
    int header_height = (int) getResources().getDimension(R.dimen.nav_header_height); 

    // Gets required display metrics 
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); 
    float screen_height = displayMetrics.heightPixels; 

    // Height of menu 
    int menu_height = (int) (screen_height - header_height); 

    // Layout params for menu 
    LayoutParams params = new LayoutParams(
      LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT); 
    params.gravity = Gravity.BOTTOM; 
    params.height = menu_height; 
    view.setLayoutParams(params); 
} 

}

Und dies in der onCreate der Haupttätigkeit

// Inflates the nav header 
    CustomNavigationView navigationView = (CustomNavigationView) findViewById(R.id.your_nav_view); 
    navigationView.createHeader(R.layout.your_nav_header); 

    // sizes nav drawer menu so it appears under header 
    ViewGroup parent = (ViewGroup) navigationView; 
    View view = parent.getChildAt(0); 
    navigationView.sizeMenu(view); 
-1

ich seine Zu alt Frage wissen aber es ist zu einfach.

<android.support.design.widget.NavigationView 
      android:id="@+id/navigation_view" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      app:headerLayout="@layout/header" 
      app:menu="@menu/drawer" /> 
    </android.support.v4.widget.DrawerLayout> 

Änderung es unter Code (include header inside)

<android.support.design.widget.NavigationView 
      android:id="@+id/navigation_view" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      app:menu="@menu/drawer" /> 

       <include layout="@layout/header"/> 
    </android.support.v4.widget.DrawerLayout> 

Arbeit wie Charme glücklich Codierung :)

0

ich auch thhis Problem und es gelöst konfrontiert. hier ist die Lösung

1) Standard-Navigationsleiste in Ihrem Projekt erstellen

2) Header-Tag von NavigationView in activity_main entfernen.xml und fügen Header innerhalb NavigationView

<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" >>>>>> X 
    app:menu="@menu/activity_main_drawer" > 
    <include 
     layout="@layout/nav_header_main" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</android.support.design.widget.NavigationView> 

3) hinzufügen erforderlich leer Artikel in oben auf Menü-> activity_main_drawer.xml (depand auf Kopfhöhe

0

löste ich dieses Problem durch

<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/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

//where your main content goes. 
<include 
    layout="@layout/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<include 
    layout="@layout/nav_drawer_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" /> 
tun

Schubladeninnenlayout inklusive dieses Layout hinzufügen,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/nav_drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

//your header layout. 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:gravity="bottom" 
    android:orientation="vertical"> 

    <include layout="@layout/layout_drawer_header"></include> 

</LinearLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/drawerRecyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:layout_weight="1" 
    android:background="#ffffff" /> 

    //your footer layout 
<include layout="@layout/nav_footer_item" /> 

Dann füllen Sie einfach die Recyclerview.