我想在抽屉布局的 ListView 中将自定义 View 显示为空 View ! 我像这样在 xml 中创建一个 View 并将其命名为 view_custom_empty.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="match_parent"
android:background="#FFff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="NO RESULT"
android:textColor="#FFffffff" />
</RelativeLayout>
在我的 Activity 中,我像这样设置 ListView emptyview:
View emptyView = getLayoutInflater().inflate(R.layout.view_custom_empty, null);
ViewGroup viewGroup = (ViewGroup) drawerLayout.getParent();
viewGroup.addView(emptyView, 0);
drawerListview.setEmptyView(emptyView);
drawerListview.setAdapter(mDrawerAdapter);
但是我的自定义空 View 在它为空时不会出现在我的 ListView 中!
最佳答案
我遇到了同样的问题,终于找到了解决办法。
你实际上可以设置一个单独的 layout.xml
作为一个空 View ,但它不会在抽屉中,而是在 Activity 本身中。
activity_main.xml
:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<!-- Your activity content here-->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Your ListView you want to be shown if not empty-->
<ListView
android:id="@+id/left_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"/>
<!-- Here starts the empty view Layout-->
<RelativeLayout
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFff0000"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="NO RESULT"
android:textColor="#FFffffff" />
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
添加empty_view
像这样添加到您的列表:
drawerListview.setAdapter(mDrawerAdapter);
drawerListview.setEmptyView(findViewById(R.id.empty_view));
逐步了解如何将 EmptyView 添加到 Navigation Drawer 中的列表
- 将您的 EmptyView(布局)添加为
<android.support.design.widget.NavigationView
的子项在抽屉导航的布局中。 - 将保留 EmptyView 的布局/ View 可见性设置为消失。在你的情况下 RealtiveLayout
android:visibility="gone"
- 将 ID 添加到第 2 点的布局/ View ,您在 Activity 中引用
- 像这样将 EmptyView 添加到您的列表中:
drawerListview.setAdapter(mDrawerAdapter); drawerListview.setEmptyView(findViewById(R.id.empty_view));
解释:
整个抽屉导航实际上在没有 <android.support.design.widget.NavigationView
的情况下工作.但是将您的列表和空 View 添加为子项,确保子项位于抽屉导航内而不是您的内容区域内。它还会自动设置抽屉导航宽度,以匹配所有屏幕尺寸的 Material 设计指南。
关于android - 如何在抽屉导航中为 ListView 设置自定义 EmptyView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24452440/