【问题标题】:Listview item is not accepting clicksListview 项目不接受点击
【发布时间】:2015-06-20 00:13:18
【问题描述】:

我有这个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textViewId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textViewText"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:singleLine="false"
                android:layout_weight="1" />


            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buttonOk"
                android:clickable="true"
                android:focusable="false"/>
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

布局用于 ListFragment。我也有以下代码:

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
    MyItem item = items.get(position);

    //Do something...
}

我有一个自定义适配器和一个查看器。但列表视图项目不可点击。目前我仍然没有按钮本身的任何代码。我也希望能够点击整个列表视图项。

上述方法不正确吗?

【问题讨论】:

  • 您使用的是自定义适配器吗?
  • @Lady_ari 是的。使用 ViewHolder
  • 我建议尝试在父 RelativeLayout 上设置一个 onClickListener

标签: android android-layout android-listview


【解决方案1】:

在您的 ListView 中,使用 setOnItemClickListener。

这是在 ListView 上处理点击事件的最简单方法

【讨论】:

  • 在 ListFragment 还是 ViewHolder 上?
  • 试试 ListFragment
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
相关资源
最近更新 更多