列表视图背景单击



当用户尝试单击我的空列表中的背景时,我正在尝试处理onClick事件视图 - 即没有项目。 ADK 不想允许我将 onClickListener 添加到 ListView,而是坚持认为添加 onItemClickListener 是要走的路 - 但显然这不适用于空列表。 我正在使用带有 xml描述符的列表活动 列表项。

任何人都可以建议最好的方法吗?

   <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:fillViewport="true" >
    <TextView
        android:id="@+id/android:item_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:lines="3" />
</RelativeLayout>

您可以添加一些视图,当您的列表为空时,将显示这些视图。此视图的 id 必须为"空"。(http://developer.android.com/reference/android/app/ListActivity.html)尝试添加单击以获取此特殊视图。

你应该尝试这样的事情:

<FrameLayout
    android:id="@+id/emptyList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ListView />
</FrameLayout>

在 Java 代码中,您可以将 onClickListener 添加到 emptyList 对象中,如果 ListView 的项计数大于 0,则返回 false(允许单击传递视图层次结构并被onItemClickListener拾取)。

在具有列表视图的布局中添加以下内容:

<ListView 
android:id="@android:id/list" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
/>
<TextView 
android:id="@android:id/empty" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:text="No Results" 
/>

现在将 onClickListener 添加到此文本视图中。

当 id 为 @android:id/list 的列表视图为空或尚未填充时,将自动显示此文本视图

最新更新