天天看點

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之前。否則是不會顯示的