gpt4 book ai didi

android - 如果 ListView 中没有项目,ListView.addFooterView() 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:40 25 4
gpt4 key购买 nike

我正在使用 listview 添加页脚,

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

这是我在 xml 中的 ListView

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

和页脚 View 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>

如果 ListView 中有项目,则成功添加 footerview.xml。如果 ListView 中没有项目,页脚 View 也不会显示。

有人可以帮忙吗?

最佳答案

我认为不可能向空的 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);

这里 emptyView 类型,所以它可以是 LinearLayoutRelativeLayout 可以使用LayoutInflater 在运行时。

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

希望对您有所帮助。

关于android - 如果 ListView 中没有项目,ListView.addFooterView() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23510168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com