Android 带有列表视图的导航视图的自定义列表
我有一个导航视图,我想在菜单中创建自定义列表,我想项目菜单有图像和文本等。我搜索了,但没有一个解决方案不适合我。我写了以下代码:Android 带有列表视图的导航视图的自定义列表,android,listview,navigation-drawer,navigationview,Android,Listview,Navigation Drawer,Navigationview,我有一个导航视图,我想在菜单中创建自定义列表,我想项目菜单有图像和文本等。我搜索了,但没有一个解决方案不适合我。我写了以下代码: <?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
<?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;
}
}
});
受保护的void onStart(){
super.onStart();
//创建导航菜单
抽屉布局=findViewById(R.id.类别\活动\抽屉布局);
toggle=newactionBarDrawerToggle(这个,抽屉布局,R.string.open,R.string.close);
抽屉布局。添加抽屉链接(切换);
toggle.syncState();
最终ActionBar ac=getSupportActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(OfflineData.getUserTitle(KitiApplication.getappContext());
NavigationView NavigationView=findViewById(R.id.category\U activity\U nv);
setBackgroundColor(getResources().getColor(R.color.navyBackgroundColor));
navigationView.setNavigationItemSelectedListener(新建navigationView.OnNavigationItemSelectedListener()){
@凌驾
公共布尔值onNavigationItemSelected(MenuItem项){
int id=item.getItemId();
开关(id){
案例R.id.mitm\U注销:
logoutUser(getApplicationContext());
打破
案例R.id.mitm_规则:
Intent rules menualActivityIntent=新的Intent(getApplicationContext(),
规则菜单类);
startActivity(规则手动内容);
覆盖过渡(R.anim.slide_up,R.anim.stay);
打破
案例R.id.mitm\u刷新列表:
字符串activityName=getActivityName();
if(activityName!=null&&activityName.equalsIgnoreCase(“.CategoryActivity”))
CategoryActivity.checkLoadTasks();
else if((activityName!=null&&activityName.equalsIgnoreCase(“.KITILTasksListActivity”))
KitilTaskListActivity.checkLoadTasks();
打破
案例R.id.mitm_更新信息:
CategoryActivity.openUpdateInfoActivity();
打破
违约:
打破
}
抽屉();
返回true;
}
});
视图头视图=导航视图。充气头视图(右布局。导航头);
navigationView.removeHeaderView(navigationView.getHeaderView(0));
ImageView imgNavHeader=headerView.findViewById(R.id.profile\u image\u navHeader);
位图profilePic=HelperMethods.getProfilePicture(此);
如果(profilePic!=null)
imgNavHeader.setImageBitmap(profilePic);
其他的
imgNavHeader.setImageDrawable(getResources().getDrawable(R.drawable.defaultprofile));
ListView listItems=findViewById(R.id.navigationMenu\u categoryActivity);
//向菜单项添加值
List menuItems=createListItems();
MenuItemAdapter=新MenuItemAdapter(此,R.layout.nav_标题_项,menuItems);
setAdapter(itemAdapter);
listItems.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
MenuItemTemp=(MenuItems)parent.getItemAtPosition(position);
开关(itemTemp.id){
案例1:
logoutUser(getApplicationContext());
打破
案例2:
Intent rules menualActivityIntent=新的Intent(getApplicationContext(),
规则菜单类);
startActivity(规则手动内容);
覆盖过渡(R.anim.slide_up,R.anim.stay);
打破
案例3:
字符串activityName=getActivityName();
if(activityName!=null&&activityName.equalsIgnoreCase(“CategoryActivity”))
CategoryActivity.checkLoadTasks();
else if((activityName!=null&&activityName.equalsIgnoreCase(“KITILTasksListActivity”))
KitilTaskListActivity.checkLoadTasks();
打破
案例4:
CategoryActivity.openUpdateInfoActivity();
打破
违约:
打破
}
}
});
上面的代码显示标题图像(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>
请将导航单元标题布局和顶部布局放在靠近抽屉布局的地方,我听不懂你说什么。你能解释一下吗?谢谢,我应该看到导航单元标题布局。添加itI编辑我的问题并添加导航单元标题。xml我想你也应该将安卓:layout\u height=“150dp”设置为包含高度