給ListView設置emptyView


給ListView設置emptyView

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

使用ListView和GridView時,當列表為空時,默認是不顯示任何內容的,這樣對用戶非常不友好,這時我們就需要通過setEmptyView(View emptyView)來設置當列表為空時的提示。

emptyView簡單使用Demo

下面展示emptyView使用的簡單Demo。

activity_empty_list_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.littlejie.listview.EmptyListViewActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

    <!-- ListView的emptyView,默認不需設置其visiblity屬性設置為GONE -->
    <!-- 當ListView為空時,emptyView會自動被設置為GONE -->
    <!-- 當ListView不為空時,emptyView會自動被設置為VIBISIBLE -->
    <LinearLayout
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Empty" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="EmptyView可以是ViewGroup" />
    </LinearLayout>

</LinearLayout>

EmptyListViewActivity.java:

public class EmptyListViewActivity extends Activity {

    private ListView mLv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty_list_view);

        mLv = (ListView) findViewById(R.id.lv);
        mLv.setAdapter(new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, generateString(0)));
        mLv.setEmptyView(findViewById(android.R.id.empty));
    }
    
    private List<String> generateString(int num) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add("item " + i);
        }
        return list;
    }

}

運行結果如下:
屏幕快照 2016-09-10 上午9.16.02

emptyView復雜使用Demo

前一個Demo講了emptyView的簡單使用方法,那如果我們要自定義emptyView或者emptyView的布局跟ListView不在同一個布局文件中呢?恩,你可能會說:直接調用setEmptyView(View emptyView)不行嘛,難道有啥不一樣?
看來,你還是too young too simple。直接調用的結果就是:emptyView根本就不會顯示!!!
新建一個ComplexEmptyListViewActivity.java:

public class ComplexEmptyListViewActivity extends Activity {

    private ListView mLv;
    private EmptyView mEmptyView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complex_empty_list_view);

        mLv = (ListView) findViewById(R.id.lv);
        mLv.setAdapter(new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, Utils.generateString(0)));

        mEmptyView = new EmptyView(this);
        //此處是重點,如果不將mEmptyView添加進當前的ViewGroup,mEmptyView都不可見
        //((ViewGroup) mLv.getParent()).addView(mEmptyView);
        mLv.setEmptyView(mEmptyView);
    }
}

布局文件activity_complex_empty_list_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.littlejie.listview.EmptyListViewActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

EmptyView:

public class EmptyView extends LinearLayout {

    public EmptyView(Context context) {
        super(context);
        init(context);
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.complex_empty_view, this);
    }
}

complex_empty_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="假裝我是復雜的emptyView" />

</LinearLayout>

運行demo,你會發現ListView為空時,根本就沒顯示emptyView。這是為什么呢?因為emptyView根本就沒添加到當前Activity的布局中,所以即使設置了emptyView也不會顯示,解決方法就是把emptyView添加到當前Activity的布局中(ComplexEmptyListViewActivity注釋的內容)

補充

  1. 細心的可能已經發現,在兩個demo中,我們都沒去設置emptyView的visiblity屬性,這是為什么呢?其實看下源碼就知道了~

    當調用setEmptyView(View emptyView)方法時,ListView會調用到updateEmptyStatus(boolean empty),在這里系統會跟empty去判斷是否顯示emptyView。恩,就這么簡單~

    /**
     * Sets the view to show if the adapter is empty
     */
    @android.view.RemotableViewMethod
    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
    
        // If not explicitly specified this view is important for accessibility.
        if (emptyView != null
                && emptyView.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
            emptyView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
        }
    
        final T adapter = getAdapter();
        final boolean empty = ((adapter == null) || adapter.isEmpty());
        updateEmptyStatus(empty);
    }
    
    /**
     * Update the status of the list based on the empty parameter.  If empty is true and
     * we have an empty view, display it.  In all the other cases, make sure that the listview
     * is VISIBLE and that the empty view is GONE (if it's not null).
     */
    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);
        }
    }
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM