OnItemClickListener 不适用于包含按钮的 ListView 项目
我有 ListView
和自定义 Adapter
,它以这种方式向 ListView
提供 View
:
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
它就可以工作。
我真的被这个问题困扰了,我尝试了各种布局,对齐,换行等等 - 无用。看起来 CheckBox
干扰 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;
}
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我的 ListView 中也遇到了 Button 的问题。不幸的是,仅仅将适配器中所有对象的焦点设置为 false 对我来说不起作用。
我现在有一个解决方法。
如果您还没有这样做,请在您的适配器中为按钮(或其他可点击对象)创建一个 OnClickListener。在该 OnClickListener 中,您自己调用 OnItemClickListener。
这确实意味着您需要让适配器访问父 ListView 和 OnItemClickListener。
I had also had the problem of a Button in my ListView. Unfortunately just setting the focus to false for all objects in my Adapter did not work for me.
I now have a workaround.
In your Adapter create an OnClickListener for the button (or other clickable object) if you have not already done that. In that OnClickListener you call the OnItemClickListener yourself.
It does mean that you will need to give your adapter access to both the parent ListView and the OnItemClickListener.
您可以考虑在列表视图项中编写 onOnTouchEvent 并将正确的 touchEvent 发送到您的子视图(按钮)。
You can consider to write your on OnTouchEvent in your listview item and send the proper touchEvent to you child view , the button .
好吧,我知道以上解决方案都不起作用。我尝试更改 xml 属性,但这些不起作用,但我以新的方式实现了它。
方法如下:
使用方法
onCheckBoxChecked
创建一个接口CheckBoxOnCheckListener
并传递所需的参数,在您的中实现接口
CheckBoxOnCheckListener
em>activity 或包含 listView 的 fragment。接下来在适配器中,将
mListener
声明为CheckBoxOnCheckListener
,并将其作为参数从fragment/activity
传递给适配器的构造函数,并将其强制转换为 < code>CheckBoxOnCheckListener 并分配给mListener
。接下来将
mListener
设置为itemView.onClick
或CheckBox.onCheckCheckedListener
并调用onCheckChanged
方法mListener.onCheckBoxChecked< /代码>。
就是这样。它肯定会起作用,它对我有用。
想要代码就pm吧。
Well i know none of the above solutions will work.I tried changing xml attributes but those does not work out, But i implemented it in a new fashion.
Here is how:
Create an interface
CheckBoxOnCheckListener
with methodonCheckBoxChecked
and pass needed parameters, implement interfaceCheckBoxOnCheckListener
in your activity or fragment containing listView.Next in your adapter, declare an
mListener
asCheckBoxOnCheckListener
, and pass this as a parameter to Adapter's constructor fromfragment/activity
and cast it toCheckBoxOnCheckListener
and assign tomListener
.Next set
mListener
asitemView.onClick
orCheckBox.onCheckCheckedListener
andonCheckChanged
method callmListener.onCheckBoxChecked
.That's it. It will definitely work,it worked for me.
For code just pm.
如果您在
Activity
中使用ListView
,请确保您已设置setOnItemClickListener()
If you are using
ListView
inActivity
, ensure you have setupsetOnItemClickListener()
只需将此行添加到项目视图中,而不是 listView 本身,
请从 Android 自定义 ListView 无法单击项目查看有关此内容的更多详细信息
just add this line into the item views instead of listView itself
check more detail about this from Android custom ListView unable to click on items
如果列表项中有 ImageButtons,则需要添加:
到根列表项元素[例如根布局]。
然后在列表项中的每个 ImageButton 中,您需要添加:
这对我有用 - 但我使用的是 ImageButton,而不是标准按钮。
If you have ImageButtons inside the list item, you need to add:
to the root list item element [such as the root layout].
Then within each ImageButton in the list item, you need to add:
This worked for me - but I was using ImageButtons, not the standard button.
我也遇到了同样的问题,我尝试将
android:focusable="false"
设置为 listview 但它不起作用,然后我将其添加到 listview 项目中..就像在我的 listview 项目中一样使用造成问题的切换按钮,我将android:focusable="false"
添加到项目单击侦听器上的切换按钮和列表视图再次开始工作I have also faced the same issue I have tried to set
android:focusable="false"
to listview but it don't work then I add this to listview item.. like in my listview item I have uesed Toggle button which was creating problem, I addandroid:focusable="false"
to Toggle button and listview on item click listener start work again将以下行添加到您的列表视图
或确保将以下行设置到您的布局文本字段
Add following line to your listView
or make sure to set following lines to your layout text fields