android - 如何在抽屉导航中为 ListView 设置自定义 EmptyView

标签 android listview navigation-drawer

我想在抽屉布局的 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 中的列表

  1. 将您的 EmptyView(布局)添加为 <android.support.design.widget.NavigationView 的子项在抽屉导航的布局中。
  2. 将保留 EmptyView 的布局/ View 可见性设置为消失。在你的情况下 RealtiveLayout android:visibility="gone"
  3. 将 ID 添加到第 2 点的布局/ View ,您在 Activity 中引用
  4. 像这样将 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/

相关文章:

android - 如何创建下拉列表?

android - 抽屉中的 SwipeRefreshLayout 手势检测

java - Android:为 recyclerView 项目添加边框和舍入

安卓SDK : What's the license of the included icons?

java - 具有自定义适配器的ListView子类: basic Java Q

wpf - 单击按钮时向 ListView 中的选定行添加删除线

android - 适配chrisbanes Action Bar-Pull To Refresh to Fragments(Navigation Drawer)

android - 抽屉导航刷新/重新加载

java - ListView 中的 ImageView 忽略所有布局尺寸并填满整个屏幕

java - ListView 适配器的 null 异常