首页 > 代码库 > 当ListView为空时,显示特定的View

当ListView为空时,显示特定的View

当ListView的Adapter没有数据时,我们可以让它显示一个“暂无数据”的View,这个View可以是TextView或者其他...

例如:



具体代码:

xml代码:

<ListView
        android:id="@+id/lv_faq"
		android:layout_width="match_parent"
		android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tv_listview_empty"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:drawableLeft="@drawable/listitem_empty"
        android:drawableStart="@drawable/listitem_empty"
        android:gravity="center"
        android:text="@string/listitem_empty"
        android:textColor="@color/listitem_little_gray"
        android:textSize="@dimen/text_size_15"
        android:textStyle="bold" />

java代码:

mFAQListView = (ListView)mFaqView.findViewById(R.id.lv_faq);
TextView emptyView = (TextView)mFaqView.findViewById(R.id.tv_listview_empty);
mFaqList = getData();
mAdapter = new ListViewFaqAdapter(getActivity(), mFaqList);
mFAQListView.setAdapter(mAdapter);
mFAQListView.setEmptyView(emptyView);






当ListView为空时,显示特定的View