【发布时间】:2017-08-05 18:40:05
【问题描述】:
我有以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true"
android:onClick="openGithubUrl">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
我想在 LinearLayout(@+id/list-item) 上添加一个 onCLick 监听器。这可能吗? onClick 触发后,我想获取子 Textview @+id/github_url 的内容。
【问题讨论】:
-
这是在 ListView 还是 RecyclerView 中?如果是这样,您不要在线性布局元素上设置点击侦听器
-
是的,它在 ListView 中
-
然后你使用
listView.setOnItemClickListener,用于点击适配器项,而不是在LinearLayout上设置点击监听 -
好的,非常感谢
标签: java android xml android-layout android-linearlayout