android: ListView设置emptyView 误区

使用ListVIew 来设置EmptyView的时候须注意:

ListView listview = (ListView) findViewById(R.id.list);
        View emptyView = findViewById(R.id.empty);
        ViewGroup parentView = (ViewGroup) listview.getParent();
        parentView.addView(emptyView, 2); // 一定要将EmptyView添加到ListView的ParentView上,不然无效果
        listview.setEmptyView(emptyView)

如果你直接在XML中包含了emptyView,那么也就不需要再额外添加到Viewhierarchy中了,因为它已经在那儿了。

例:

<?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"
    >


    <View
        android:layout_width="match_parent"
        android:layout_height="45dip"
        android:background="#CCC"
        />

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/transparent"
        />

    <TextView
        android:id="@+id/tv_empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="No Data"
        android:textColor="@android:color/black"
        android:visibility="visible"
        />

</LinearLayout>
ListViewApiUseDemoActivity.java

package com.yongdaimi.android.androidapitest;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewApiUseDemoActivity extends AppCompatActivity {


    private ListView lv_list;
    private TextView tv_empty_view;

    private ArrayAdapter<String> arrayAdapter;


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

       /* tv_empty_view = new TextView(this);
        tv_empty_view.setText("No Data");
        tv_empty_view.setGravity(Gravity.CENTER);
        ViewGroup viewGroup = (ViewGroup) lv_list.getParent();
        viewGroup.addView(tv_empty_view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
*/
        lv_list.setEmptyView(tv_empty_view);
        String[] emptyData = new String[]{};
        arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, emptyData);
        lv_list.setAdapter(arrayAdapter);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
                arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data);
                lv_list.setAdapter(arrayAdapter);
            }
        }, 1500);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String[] data = {};
                arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data);
                lv_list.setAdapter(null);
            }
        }, 4500);

    }

    private void initView() {
        lv_list = findViewById(R.id.lv_list);
         tv_empty_view = findViewById(R.id.tv_empty_view);
    }

}

posted @ 2019-06-19 13:55  夜行过客  阅读(609)  评论(0编辑  收藏  举报