【问题标题】:Empty View does not display anything空视图不显示任何内容
【发布时间】:2017-08-08 05:50:22
【问题描述】:

基本上我有一个显示一堆项目的ListView(lv),如果lv为空则显示EmptyView,但它什么也不显示代码:

ListView l = (ListView) view.findViewById(R.id.ArrayList);
    l.setAdapter(adapter);
    srl = (SwipeRefreshLayout) view.findViewById(R.id.refreshView);
    srl.setOnRefreshListener(srfListener());
    View Empty = inflater.inflate(R.layout.empty,null);
    l.setEmptyView(Empty);

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:text="Nothing Available "
    android:textSize="30sp"
    android:textColor="@color/colorPrimary"
    android:layout_height="match_parent"
    >
</TextView>

任何想法来解决这个问题?顺便说一句,它不是列表活动。

【问题讨论】:

标签: java android xml


【解决方案1】:

根据您的代码,您正在创建视图并在列表中添加为 emptyView,

View Empty = inflater.inflate(R.layout.empty,null);
l.setEmptyView(Empty);

但是您可以在下面看到实际代码,它只是让 VISIBLE 和 GONE 消失,它没有添加到您的布局中。

private void updateEmptyStatus(boolean empty) {
        if (isInFilterMode()) {
            empty = false;
        }

        if (empty) {
            if (mEmptyView != null) {
                mEmptyView.setVisibility(View.VISIBLE);
                setVisibility(View.GONE);
            } else {
                // If the caller just removed our empty view, make sure the list view is visible
                setVisibility(View.VISIBLE);
            }

            // We are now GONE, so pending layouts will not be dispatched.
            // Force one here to make sure that the state of the list matches
            // the state of the adapter.
            if (mDataChanged) {           
                this.onLayout(false, mLeft, mTop, mRight, mBottom); 
            }
        } else {
            if (mEmptyView != null) mEmptyView.setVisibility(View.GONE);
            setVisibility(View.VISIBLE);
        }
    }

因此,将其添加到您的布局中,然后将该视图提供给 setEmptyView

你可以这样添加,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:background="@color/dark_bg_color"
    android:gravity="top"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/recentcall_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="@color/transparent"
        android:divider="#cecece"
        android:dividerHeight="1dp"
        android:listSelector="@color/transparent"
         />

    <ViewStub
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:layout="@layout/[your empty layout]" />

</LinearLayout>

在你的java代码中,你可以这样调用,

ViewStub emptyViewStub = (ViewStub) view.findViewById(android.R.id.empty);
l.setEmptyView(emptyViewStub);

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多