OnItemClickListener 不适用于包含按钮的 ListView 项目 [英] OnItemClickListener doesn't work with ListView item containing button
问题描述
我有带有自定义 Adapter
的 ListView
,它以这种方式将 View
提供给 ListView
:
I have ListView
with custom Adapter
which supplies View
to ListView
in this way:
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout.LayoutParams lineParams;
RelativeLayout line=new RelativeLayout(context);
TextView tv=new TextView(context);
tv.setText("Text in postion="+i);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
line.addView(tv, lineParams);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//checkbox
CheckBox checkBox=new CheckBox(context);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
line.addView(checkBox, lineParams);
return line;
}
在ListView
的某处有setOnItemClickListener()
,它应该拦截项目点击事件.我的问题是,每当我尝试向项目添加复选框时 - 我的 ListView
都没有得到任何响应.如果我跳过 CheckBox
或任何其他 Button
它工作.
And somewhere inside ListView
there's setOnItemClickListener()
, which should intercept item clicking events. My problem that, whenever I try to add checkbox to item - I don't get any responces from my ListView
. If I skip CheckBox
or any other Button
it works.
我真的被这个问题困住了,我尝试了各种布局、对齐、环绕等 - 没用.看起来 CheckBox
干扰了 ListView
项目点击事件.
I am really stuck with this problem, I have tried all kind of Layouts, aligning, wrapping and so on - useless. Looks like CheckBox
interferes ListView
item click events.
有什么克服方法的想法吗?
Any ideas how to overcome?
推荐答案
只需将此行添加到项目视图中,而不是将其添加到 listView 本身
just add this line into the item views instead of listView itself
android:focusable="false"
从 Android 自定义 ListView 无法点击项目查看更多详细信息
这篇关于OnItemClickListener 不适用于包含按钮的 ListView 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!