如果listview中没有项目,listview. addfooterview()不起作用



我正在添加页脚到listview使用

View footerview = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_social, null, false);            
listview_followers.addFooterView(footerview);

这是我的listview在xml

<ListView
    android:id="@+id/listview_followers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

And footerview xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btnSend_social"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/botao_bgg"
        android:text="SEND"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:textStyle="bold" />
</LinearLayout>
如果列表视图中有条目,

footerview.xml成功添加。如果listview中没有条目,footerview也不会显示

有人能帮帮忙吗?

我不认为它可能添加一个页脚到一个空的ListView。虽然ListView可以使用空的View,当ListView中没有项目时可以显示。现在有两种方法可以做到:

1:

RelativeLayout empty = (RelativeLayout) getLayoutInflater().inflate(
           R.layout.empty_view,
           null);
((ViewGroup)list.getParent()).addView(empty);
list.setAdapter(adapter);

2:

RelativeLayout empty = (RelativeLayout) getLayoutInflater().inflate(
           R.layout.empty_view,
           (ViewGroup) list.getParent());
list.setEmptyView(empty);

这里empty的类型是View,所以它可以是LinearLayoutRelativeLayout,可以在运行时使用LayoutInflater进行膨胀。

注意:确保在添加空视图后设置适配器。

希望能有所帮助。

最新更新