【问题标题】:Android TableView as itemview in ListView not clickableAndroid TableView作为ListView中的itemview不可点击
【发布时间】:2011-01-27 13:31:26
【问题描述】:

我正在使用带有 ListView 的自定义适配器并使用自定义布局来提供列表项视图。我的布局是这样的

> LinearLayout
    > TableView
        > TableRow
            > TextView
            > TextView

这会导致列表项不响应点击,但如果我将布局编辑为以下内容,它会起作用

> LinearLayout
    > TextView
    > TextView

所以我知道我的适配器和活动没有问题。

我已尝试按照此问题ListView items not clickable with HorizontalScrollView inside 中的建议将android:descendantFocusability="blocksDescendants" 添加到顶级线性布局中

我也尝试将android:clickable="true"android:focusable="true" 添加到TableView,但这也不起作用。

有什么想法吗?


编辑

布局 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="wrap_content">
    <TableLayout  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    >
        <TableRow 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:clickable="true"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:gravity="center_vertical">
            <TextView 
                android:id="@+id/ITEMVIEW_TEXT2_lblText" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp" 
                android:paddingRight="5dp"
                android:paddingLeft="15dp"
                />
            <TextView 
                android:id="@+id/ITEMVIEW_TEXT2_lblText2" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="#ff385487"
                android:gravity="right"
                android:paddingRight="15dp"
                />
        </TableRow>
    </TableLayout>
</LinearLayout>

编辑

我已尝试按照建议的herehere 添加android:focusable="false",但这也不起作用。


编辑

我真正想要在这里实现的只是布置两列文本,一列左对齐,另一列右对齐。我想出了一个更好的布局来实现这一点,它不使用 TableView,所以我的 ListView 项目仍然可以点击,它也使用更少的视图,所以它也会更高效,就是这样

<?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="wrap_content"
    android:orientation="horizontal" >
            <TextView 
                android:id="@+id/ITEMVIEW_TEXT2_lblText" 
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textSize="20sp" 
                android:paddingRight="5dp"
                android:paddingLeft="15dp"
                />
            <TextView 
                android:id="@+id/ITEMVIEW_TEXT2_lblText2" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="#ff385487"
                android:gravity="right"
                android:paddingRight="15dp"
                />
</LinearLayout>

这不是解决 TableView 使 ListView 项目不可点击的问题的解决方案,但我在此处已指出,以防其他人想弄清楚如何进行类似的布局。

【问题讨论】:

  • 可以粘贴listview布局的xml代码吗?

标签: android android-listview


【解决方案1】:

我在使用 TableLayout 和 TableRow 的组合时遇到了类似的问题。

我的解决方案如下: TableRow 似乎没有自己的选择器,因此即使您将其定义为可点击和可聚焦,您也永远不会看到视觉差异。 我为 TableRow 分配了一个与 GridView 使用的背景相当的背景(可以在 SDK 中的 android-sdk-windows\platforms\android-10\data\res\drawable\grid_selector_background.xml 下找到)。

之后它完全可以聚焦和点击。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多