天天看点

ListView添加footView不显示问题

首先先上解决问题之后的代码:

这是我的footView :include_listview_footview

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

</LinearLayout>
           

代码:

View footView = View.inflate(mActivity, R.layout.include_listview_footview, null);//footView
            list.addFooterView(footView, null, false);
           
//headview
            View headView = View.inflate(mActivity, R.layout.<span style="font-family: Arial, Helvetica, sans-serif;">include_</span><span style="font-family: Arial, Helvetica, sans-serif;">list_headview, null);</span>
            initHeadView(headView);
            list.addHeaderView(headView);
           
myListAdapter = new MyListAdapter(list, mActivity);
            list.setAdapter(myListAdapter);
           

之前的footView布局文件:

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

从footview布局中可以很清楚看出问题:为跟布局控件设置高度是无效的,需要为footview的子控件设置高度才会显示。

当然还有另外的解决办法,如果footView不需要子控件,可以给根布局设置一个背景图片,这样也是可以的

需要注意的一点就是list.addFooterView()和list.addHeaderView()一定要在setAdapter之前。否则是不会显示的