ListView与GridView中SetEmptyView方法的使用,及触发条件

来源:互联网 发布:java文件上传类 编辑:程序博客网 时间:2024/06/10 07:48

我们在使用ListView与GridView组件的时候通常为给用户一个友好的界面提示。在没有数据的时候我们可以给用户一个提示。我们这时候就用到了setEmptyView这个方法,

这个方法。

使用的方式:

1.我们提示的view与LIstView是同一个xml文件,我们就可以在java文件中直接找到这个当ListView是空的时候的要显示的View,使用setEmptyView方法添加,在ListView是空的时候就会自动显示

xml定义如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/listview_main"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <TextView        android:id="@+id/listview_empty"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="没有数据" /></LinearLayout>
java中的代码如下:

String[] data = getResources().getStringArray(R.array.userName);    mListViewMain = (ListView) findViewById(R.id.listview_main);    EmptyView=(TextView) findViewById(R.id.listview_empty);    mListViewMain.setEmptyView(EmptyView);    ArrayAdapter<String> adapter =        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);    mListViewMain.setAdapter(adapter);

2.们使用的ListView的xml与当ListView是空的时候显示view的xml不是同一个xml的时候,我们就需要在java代码中动态的添加

mListView = (ListView) findViewById(R.id.listview_main);    // Listview数据是null,网络不好显示的界面    View view = getLayoutInflater().inflate(R.layout.empty_net_error, null);    ((ViewGroup) mListView.getParent()).addView(view, new LayoutParams(LayoutParams.MATCH_PARENT,        LayoutParams.MATCH_PARENT));    mListView.setEmptyView(view);

    mListView = (ListView) findViewById(R.id.listview_main);    // Listview数据是null,网络不好显示的界面    View view = getLayoutInflater().inflate(R.layout.empty_net_error, null);    addContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT,        LayoutParams.MATCH_PARENT));    mListView.setEmptyView(view);
这里用到了addContentView,是不是与setContentView很像啊,addContentView是添加view到原来view的后面,不会删除原来的view,但是setcontentView是先把原来view删除,在添加一个view,也就替换。

setEmptyView的触发条件

setEmptyView的触发条件是当用户的使用的ListView都gridView的内容是空的或ListView,GridView的适配器是null的时候触发。

让我们来看看源码中是怎么写的

public void setEmptyView(View emptyView) {        mEmptyView = emptyView;        final T adapter = getAdapter();        final boolean empty = ((adapter == null) || adapter.isEmpty());        updateEmptyStatus(empty);    }

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中判断了一下适配器adapter是不是null,或listview中的内容是空,如果adapter是null,或listview内容是空,都可以触发setEmptyView的显示。

在updateEmptyStatus中控制界面的显示。如果满足了setEmptyView的显示,需要判断一下是不是设置了EmptyView的view,如果设置了EmptyView的显示,就隐藏到listview的view,如果没有设置就显示listview的界面。如果不满足setEmptyView的条件就隐藏emptyview的view,显示listview的view


2 1
原创粉丝点击