如何在Android中滚动导航菜单



我在抽屉布局中使用导航菜单。我有标题布局和菜单布局。这是我的代码。

navigation.xml

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view_staff"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home_staff"
        app:itemIconTint="#fa32a4"
        app:itemTextColor="#212121"
        app:menu="@menu/activity_home_staff_drawer" >

菜单。xml。即使我尝试在标题布局中给出所有菜单,并提供滚动浏览量,但整个标头滚动,我只想滚动菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_dashboard_staff"
            android:icon="@drawable/dashboard"
            android:title="Dashboard" />
        <item
            android:id="@+id/nav_students_staff"
            android:icon="@drawable/teachers"
            android:title="Students" />
        <item
            android:id="@+id/nav_attendance_staff"
            android:icon="@drawable/attendance"
            android:title="Attendance" />
        <item
            android:id="@+id/nav_timetable_staff"
            android:icon="@drawable/calendericon"
            android:title="Timetable" />
        <item
            android:id="@+id/nav_class_attendance_staff"
            android:icon="@drawable/attendance"
            android:title="Class Attendance" />
        <item
            android:id="@+id/nav_consumablity"
            android:icon="@drawable/attendance"
            android:title="Consumability" />
        <item
            android:id="@+id/nav_lessonplan"
            android:icon="@drawable/accounts"
            android:title="Lesson Plan" />
    </group>
 <!--   <item
        android:background="#000"
        android:title="">
        <menu>
            <item
                android:title="" />
            <item
                android:background="#000"
                android:id="@+id/nav_send5"
                android:icon="@drawable/icon_white"
                android:title="Powerd by SchoolWyse" />
        </menu>
    </item>-->
</menu>

您可以在navigationView中尝试使用listView,以完成此操作。您可能需要通过夸大元素来添加listView项目。

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

<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:background="@color/colorPrimaryDark"
    android:fitsSystemWindows="true">
    <ListView
        android:id="@+id/navList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/profileBox"
        android:background="@color/colorPrimaryDark"
        android:choiceMode="singleChoice" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

适配器供参考:

public class DrawerListAdapter extends BaseAdapter {
Context mContext;
ArrayList<NavItem> mNavItems;
public DrawerListAdapter(Context context, ArrayList<NavItem> navItems) {
    mContext = context;
    mNavItems = navItems;
}
@Override
public int getCount() {
    return mNavItems.size();
}
@Override
public Object getItem(int position) {
    return mNavItems.get(position);
}
@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.drawer_item, null);
    }
    else {
        view = convertView;
    }
    TextView titleView = view.findViewById(R.id.title);
    TextView subtitleView = view.findViewById(R.id.subTitle);
    ImageView iconView = view.findViewById(R.id.icon);
    titleView.setText( mNavItems.get(position).mTitle );
    subtitleView.setText( mNavItems.get(position).mSubtitle );
    iconView.setImageResource(mNavItems.get(position).mIcon);
    return view;
}

}

您可以使用AddHeaderview添加标题部分。

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.navList);
adapter = new DrawerListAdapter(this, mNavItems);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.nav_header_menu, mDrawerList, false);
mDrawerList.addHeaderView(header, null, false);
mDrawerList.setAdapter(adapter);

相关内容

  • 没有找到相关文章

最新更新