【问题标题】:Android list view setOnItemClickListener works only ifAndroid 列表视图 setOnItemClickListener 仅在以下情况下有效
【发布时间】:2015-02-11 11:43:40
【问题描述】:

我有一个 Android 布局。我设置了一个像list.setOnItemClickListener 这样的监听器。一切似乎都很好。

在下面一行:

<ScrollView android:layout_width="fill_parent"
            android:layout_height="wrap_content">

如果我将 android:layout_heightwrap_content 更改为 fill_parent,它将不再起作用(无法选择我列表中的项目)。

仅当它设置为wrap_content 时才有效。为什么会这样?

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@android:id/tabhost"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TabWidget android:id="@android:id/tabs"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
            />
    <FrameLayout android:id="@android:id/tabcontent"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
            >
        <ListView android:id="@+id/restaurants"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                />
        <ScrollView android:layout_width="fill_parent"
                    android:layout_height="wrap_content">

            <TableLayout android:id="@+id/details"
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content"
                         android:stretchColumns="1"
                         android:shrinkColumns="1"
                         android:paddingTop="4dip"
                    >
                <TableRow>
                    <TextView android:text="@string/name" />
                    <EditText android:id="@+id/name" />
                </TableRow>
                <TableRow>
                    <TextView android:text="@string/address" />
                    <EditText android:id="@+id/addr" />
                </TableRow>
                <TableRow>
                    <TextView android:text="Type:" />
                    <RadioGroup android:id="@+id/types">
                        <RadioButton android:layout_height="wrap_content"
                                     android:layout_width="wrap_content"
                                     android:id="@+id/take_out"
                                     android:text="@string/takeout"
                                     android:checked="true"
                                />

                        <RadioButton android:layout_height="wrap_content"
                                     android:layout_width="wrap_content"
                                     android:id="@+id/sit_down"
                                     android:text="@string/sitdown"
                                />
                        <RadioButton android:layout_height="wrap_content"
                                     android:layout_width="wrap_content"
                                     android:id="@+id/delivery"
                                     android:text="@string/delivery"
                                />
                    </RadioGroup>
                </TableRow>

                <TableRow>
                    <TextView android:text="@string/notes" />
                    <EditText android:id="@+id/notes" />
                </TableRow>

                <Button android:id="@+id/save"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/save"
                        />
            </TableLayout>
        </ScrollView>
    </FrameLayout>
</LinearLayout>
</TabHost>

【问题讨论】:

  • 它不会起作用,因为使用fill_parent 会尝试填充FrameLayout 内的所有区域,但也存在ListView android:id="@+id/restaurants"。也许解决方案是将ScrollView 放在另一个RelativeView 中,并将其放在ListView android:id="@+id/restaurants" 下方
  • 做一个测试:将listView背景颜色改为黑色,将ScrollView背景颜色改为红色。看看你正在使用什么视图。它可以填满listView上方的所有空间。

标签: android onitemclicklistener android-wrap-content


【解决方案1】:

fill_parent(在 API 8 级及更高版本中已弃用并重命名为 MATCH_PARENT

将小部件的布局设置为 fill_parent 将强制它展开以占用其所在布局元素内的可用空间。这大致相当于将 Windows 窗体控件的停靠样式设置为 Fill .

将顶级布局或控件设置为 fill_parent 将强制它占据整个屏幕。

wrap_content

将 View 的大小设置为 wrap_content 会强制它展开到足以包含它所包含的值(或子控件)的程度。对于控件——如文本框 (TextView) 或图像 (ImageView)——这将包装正在显示的文本或图像。对于布局元素,它将调整布局大小以适应作为其子元素添加的控件/布局。

在Android代码文档here中有一些细节。

【讨论】:

    【解决方案2】:

    您的 ScrollView 占用了您的 ListView 内可以填充到您的屏幕上的所有可用空间,基本上覆盖了它。

    您永远不应该将 ScrollView 与 ListView 一起使用,因为 ListView 负责自己的垂直滚动。最重要的是,这样做会破坏 ListView 中处理大型列表的所有重要优化,因为它有效地强制 ListView 显示其整个项目列表以填充 ScrollView 提供的无限容器。 --来自http://developer.android.com/reference/android/widget/ScrollView.html

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      相关资源
      最近更新 更多