OnItemClickListener 不适用于包含按钮的 ListView 项目

发布于 2024-12-20 10:51:25 字数 1423 浏览 6 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一城柳絮吹成雪 2024-12-27 10:51:26

我的 ListView 中也遇到了 Button 的问题。不幸的是,仅仅将适配器中所有对象的焦点设置为 false 对我来说不起作用。

我现在有一个解决方法。

如果您还没有这样做,请在您的适配器中为按钮(或其他可点击对象)创建一个 OnClickListener。在该 OnClickListener 中,您自己调用 OnItemClickListener。

public void onClick(View v) {
    mOnItemClickListener.setOnItemClick(mListView, v, vPos, vId);
}

这确实意味着您需要让适配器访问父 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.

public void onClick(View v) {
    mOnItemClickListener.setOnItemClick(mListView, v, vPos, vId);
}

It does mean that you will need to give your adapter access to both the parent ListView and the OnItemClickListener.

寄人书 2024-12-27 10:51:26

您可以考虑在列表视图项中编写 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 .

放低过去 2024-12-27 10:51:26

好吧,我知道以上解决方案都不起作用。我尝试更改 xml 属性,但这些不起作用,但我以新的方式实现了它。
方法如下:

使用方法 onCheckBoxChecked 创建一个接口 CheckBoxOnCheckListener 并传递所需的参数,在您的 中实现接口 CheckBoxOnCheckListener em>activity 或包含 listViewfragment

接下来在适配器中,将 mListener 声明为 CheckBoxOnCheckListener,并将其作为参数从 fragment/activity 传递给适配器的构造函数,并将其强制转换为 < code>CheckBoxOnCheckListener 并分配给 mListener

接下来将mListener设置为itemView.onClickCheckBox.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 method onCheckBoxChecked and pass needed parameters, implement interface CheckBoxOnCheckListener in your activity or fragment containing listView.

Next in your adapter, declare an mListener as CheckBoxOnCheckListener, and pass this as a parameter to Adapter's constructor from fragment/activity and cast it to CheckBoxOnCheckListener and assign to mListener.

Next set mListener as itemView.onClick or CheckBox.onCheckCheckedListener and onCheckChanged method call mListener.onCheckBoxChecked.

That's it. It will definitely work,it worked for me.
For code just pm.

贱人配狗天长地久 2024-12-27 10:51:26

如果您在 Activity 中使用 ListView,请确保您已设置 setOnItemClickListener()

myListView.setOnItemClickListener(this); // if your activity implement OnItemClickListener

If you are using ListView in Activity, ensure you have setup setOnItemClickListener()

myListView.setOnItemClickListener(this); // if your activity implement OnItemClickListener
那支青花 2024-12-27 10:51:25

只需将此行添加到项目视图中,而不是 listView 本身,

android:focusable="false"

请从 Android 自定义 ListView 无法单击项目查看有关此内容的更多详细信息

just add this line into the item views instead of listView itself

android:focusable="false"

check more detail about this from Android custom ListView unable to click on items

酒儿 2024-12-27 10:51:25

如果列表项中有 ImageButtons,则需要添加:

android:descendantFocusability="blocksDescendants"

到根列表项元素[例如根布局]。

然后在列表项中的每个 ImageButton 中,您需要添加:

android:focusableInTouchMode="true"

这对我有用 - 但我使用的是 ImageButton,而不是标准按钮。

If you have ImageButtons inside the list item, you need to add:

android:descendantFocusability="blocksDescendants"

to the root list item element [such as the root layout].

Then within each ImageButton in the list item, you need to add:

android:focusableInTouchMode="true"

This worked for me - but I was using ImageButtons, not the standard button.

你的心境我的脸 2024-12-27 10:51:25

我也遇到了同样的问题,我尝试将 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 add android:focusable="false" to Toggle button and listview on item click listener start work again

北音执念 2024-12-27 10:51:25

将以下行添加到您的列表视图

android:choiceMode="singleChoice"

或确保将以下行设置到您的布局文本字段

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

Add following line to your listView

android:choiceMode="singleChoice"

or make sure to set following lines to your layout text fields

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文