【问题标题】:addHeaderView does not work when the list is empty列表为空时 addHeaderView 不起作用
【发布时间】:2013-12-10 09:57:53
【问题描述】:

我正在使用以下布局(header.xml)在列表视图中添加标题,

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/greetingContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/categoryTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="header view"
    android:textColor="#FFFFFF"
    android:textSize="15sp" />

</LinearLayout>

另一方面,我正在使用,

View header = (View) getLayoutInflater().inflate(R.layout.header,null);
getListView().addHeaderView(header);

当列表为空时,它不起作用,保持不可见..

我的问题可能与this重复,但无法理解,
请帮忙!

【问题讨论】:

  • 您可以使用相对布局,在顶部有文本视图,在文本视图下方有列表视图。是的,当列表视图为空时它不会显示
  • 一切都很好,当列表包含数据时!
  • 是的,默认情况下它就是这样工作的。
  • 当列表为空时如何设置标题?
  • 阅读第一条评论或查看stackoverflow.com/questions/18127132/…

标签: android xml android-listview


【解决方案1】:

简单的解决方案,您需要做的就是始终为您的listview 设置一个适配器,即使您没有任何数据要显示。

【讨论】:

  • 这对我有用,可能应该被标记为正确答案。只需使用空数据集设置 ArrayAdapter
【解决方案2】:

我使用了类似的标题视图,当列表视图中没有数据时它会很好。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.list, null);
    listView = (ListView) view.findViewById(R.id.listView1);

    if (listView.getHeaderViewsCount() == 0) {
        headerView = View.inflate(getActivity(), R.layout.listheader,     null);
        listView.addHeaderView(headerView, null, false);
    }
    return view;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2022-01-01
    相关资源
    最近更新 更多