【问题标题】:Why my Android setOnItemClickListener doesn't work?为什么我的 Android setOnItemClickListener 不起作用?
【发布时间】:2013-11-18 20:01:57
【问题描述】:

我在设置 setOnItemClickListener 时遇到问题。以下是我的代码。我已经测试了 setAdapter 是否有效,并且列表和项目显示在 UI 上。设置 setOnItemClickListener 时,它不起作用。

cool_simpleAdapter = new SimpleAdapter(this, items,
    R.layout.mylistitem, new String[] { "title", "link" }, new int[] {
            R.id.textView_title, R.id.textView_link });
cool_listView.setAdapter(cool_simpleAdapter);
Log.d("tag_1", "before setOnItemClickListener");
cool_listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Log.d("tag_setonItemClick", "in onItemClick");
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
});
Log.d("tag_2", "after setOnItemClickListener");

我把日志用来追踪发生了什么:

Log.d("tag_1","before setOnItemClickListener");

Log.d("tag_2","after setOnItemClickListener");

显示了,但是

Log.d("tag_setonItemClick","in onItemClick");

没有显示。而且我无法单击该项目,也无法打开 URL。我不知道我应该如何解决这个问题。

编辑:添加 mylistitem.xml 布局

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <TextView
                android:id="@+id/textView_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView_link"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 你可以参考这个post,看看你有没有同样的问题。
  • 离题但我认为不需要第二个LinearLayout
  • 我没有看到您为 setOnItemClickListener 共享代码和 xml 的任何问题。可能您已经为您的 listView 设置了另一个 setOnItemClickListener 并覆盖了第一个。
  • 其实使用单一的相对布局会更好
  • 我无法使用给定的代码重现所描述的行为。对我来说,它按预期工作;即,日志条目“tag_setonItemClick”,“in onItemClick”确实会在单击时出现。也许@aegean 是正确的。

标签: android android-layout android-intent android-listview android-ui


【解决方案1】:

在这种情况下,您的 Button 已聚焦,因此侦听 setOnItemClickListener 不起作用..因此使它们可聚焦为 false .. 在

上的 Button 的 xml 中添加这一行
android:focusable="false"

【讨论】:

    【解决方案2】:

    我之前也遇到过这个问题,那是因为listview的每一项都有一个Button。 要解决这个问题,只需将按钮的android:focusable 设置为false。 希望对你有帮助

    【讨论】:

    • 也许你可以尝试使用 android:focusable=false 设置 TextView
    • 感谢@Patato 的帮助 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    相关资源
    最近更新 更多