首页 > 解决方案 > 带有列表视图的自定义导航视图列表

问题描述

我有一个导航视图,我想在菜单中创建自定义列表,我希望项目菜单有图像和文本等。我​​搜索了但没有一个解决方案对我不起作用。我写了下面的代码:

    <?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"
android:id="@+id/category_activity_drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@color/blue"
android:layoutDirection="rtl"
android:textDirection="rtl">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">

    <GridView
        android:id="@+id/categoryGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/my_toolbar"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:padding="10dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"></GridView>
</LinearLayout>

<!--    </LinearLayout>-->

<android.support.design.widget.NavigationView
    android:id="@+id/category_activity_nv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/lightGray"
    app:itemTextColor="@android:color/black">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/nav_header"></include>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="texstttt"
            android:id="@+id/text">
        </TextView>

        <ListView
            android:id="@+id/navigationMenu_categoryActivity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@color/blue" />
    </LinearLayout>

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

活动:

      protected void onStart() {
    super.onStart();

    //Create Navigation menu
    drawerLayout = findViewById(R.id.category_activity_drawerlayout);
    toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    final ActionBar ac = getSupportActionBar();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(OfflineData.getUserTitle(KITILApplication.getappContext()));

    NavigationView navigationView = findViewById(R.id.category_activity_nv);
    navigationView.setBackgroundColor(getResources().getColor(R.color.navyBackgroundColor));
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected( MenuItem item) {
            int id = item.getItemId();
            switch (id) {
                case  R.id.mitm_logout:
                    HelperMethods.logoutUser(getApplicationContext());
                    break;
                case R.id.mitm_Rules:
                    Intent rulesMenuActivityIntent = new Intent(getApplicationContext(),
                            RulesMenu.class);
                    startActivity(rulesMenuActivityIntent);
                    overridePendingTransition(R.anim.slide_up, R.anim.stay);
                    break;
                case R.id.mitm_refresh_list:
                    String activityName = getActivityName();
                    if (activityName != null && activityName.equalsIgnoreCase(".CategoryActivity"))
                        CategoryActivity.checkLoadTasks();
                    else if ((activityName != null && activityName.equalsIgnoreCase(".KITILTasksListActivity")))
                        KITILTasksListActivity.checkLoadTasks();
                    break;
                case  R.id.mitm_UpdateInfo:
                    CategoryActivity.openUpdateInfoActivity();
                    break;
                default:
                    break;
            }
            closeDrawer();
            return true;
        }
    });

    View headerView = navigationView.inflateHeaderView(R.layout.nav_header);
    navigationView.removeHeaderView(navigationView.getHeaderView(0));
    ImageView imgNavHeader = headerView.findViewById(R.id.profile_image_navHeader);
    Bitmap profilePic = HelperMethods.getProfilePicture(this);
    if (profilePic != null)
        imgNavHeader.setImageBitmap(profilePic);
    else
        imgNavHeader.setImageDrawable(getResources().getDrawable(R.drawable.defaultprofile));


    ListView listItems = findViewById(R.id.navigationMenu_categoryActivity);

    //Add Values to menu items
    List<MenuItems> menuItems = createListItems();
    MenuItemAdapter itemAdapter = new MenuItemAdapter(this, R.layout.nav_header_items, menuItems);
    listItems.setAdapter(itemAdapter);
    listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MenuItems itemTemp = (MenuItems) parent.getItemAtPosition(position);
            switch (itemTemp.id) {
                case 1:
                    HelperMethods.logoutUser(getApplicationContext());
                    break;
                case 2:
                    Intent rulesMenuActivityIntent = new Intent(getApplicationContext(),
                            RulesMenu.class);
                    startActivity(rulesMenuActivityIntent);
                    overridePendingTransition(R.anim.slide_up, R.anim.stay);
                    break;
                case 3:
                    String activityName = getActivityName();
                    if (activityName != null && activityName.equalsIgnoreCase("CategoryActivity"))
                        CategoryActivity.checkLoadTasks();
                    else if ((activityName != null && activityName.equalsIgnoreCase("KITILTasksListActivity")))
                        KITILTasksListActivity.checkLoadTasks();
                    break;
                case 4:
                    CategoryActivity.openUpdateInfoActivity();
                    break;
                default:
                    break;
            }
        }
    });

上面的代码显示标题图像(nav_header.xml),但不显示列表视图或文本视图。为什么?以及如何解决这个问题?

如果我将菜单添加到导航视图,导航视图项目显示正确,但如果我删除它并设置列表视图,则不会显示任何内容!

nav_header.xml

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="#0e86b0"
    android:gravity="right">

    <RelativeLayout
        android:id="@+id/mainLayoutOfImage_navHeader"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:layout_marginRight="15dp">

        <ImageView
            android:id="@+id/imageView14"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/background_circular_imageview" />

        <ImageView
            android:id="@+id/profile_image_navHeader"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/defaultprofile" />
    </RelativeLayout>

</RelativeLayout>

标签: androidlistviewnavigation-drawernavigationview

解决方案


推荐阅读