Java ListView OnItemClickListener 没有响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2367936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:37:08  来源:igfitidea点击:

ListView OnItemClickListener Not Responding?

javaandroidlistview

提问by Kleptine

I've looked everywhere for a solution to this, but I can't figure out how to implement it. My OnItemClickListenerwas disabled somehow on my ListViewrows, because I have an ImageButtonin the row layout, which takes over the focus. There have been numerous questions I've found, but none of them have gotten me anywhere.

我到处寻找解决方案,但我不知道如何实现它。我的行OnItemClickListener以某种方式被禁用ListView,因为我有一个ImageButton行布局,它接管了焦点。我发现了很多问题,但没有一个让我得到任何结果。

I've checked thisquestion, but I couldn't really make heads or tails of it. I just need a way to get the rows clickable so that I can detect when a row is pressed. Long press and focus work fine.

我已经检查过这个问题,但我无法真正理解它。我只需要一种方法来使行可点击,以便我可以检测何时按下一行。长按和对焦工作正常。

采纳答案by jqpubliq

Instead of an OnItemClickListener, add an OnClickListenerto each of your views returned from your adapter. You'll need to use setItemsCanFocussetting up your list:

代替的OnItemClickListener,添加一个OnClickListener到您的每一个意见从适配器返回。您需要使用setItemsCanFocus设置您的列表:

ListView list = (ListView) findViewById(R.id.myList);
list.setAdapter(new DoubleClickAdapter(this));
list.setItemsCanFocus(true);

and then in your Adapter's getView, this will yield a clickable row. The button is assumed to be in the inflated xml.

然后在你的适配器getView,这将产生一个可点击的行。假定按钮位于膨胀的 xml 中。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = View.inflate(context, R.layout.cell, null);
    view.setClickable(true);
    view.setFocusable(true);
    view.setBackgroundResource(android.R.drawable.menuitem_background);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context).setTitle("touched").show();
        }

    });
    return view;
}

回答by greg7gkb

For my version of this problem, the issue was that I had set my TextViewobject to android:inputType="textMultiLine". When I removed this line the issue of the list not being clickable was gone. Looks like a nasty little bug.

对于我这个问题的版本,问题是我已将TextView对象设置为android:inputType="textMultiLine". 当我删除这一行时,列表不可点击的问题就消失了。看起来像个讨厌的小虫子。

Also, I'm still able to use the android:minLines/android:maxLinesproperties with no problem, so it's not a big issue. Just not the solution I expected.

另外,我仍然可以android:minLines/android:maxLines毫无问题地使用这些属性,所以这不是什么大问题。只是不是我期望的解决方案。

回答by humbleCoder

One alternative to setting an OnClickListenerfor every view is to NOT use an ImageButton- use an ImageViewinstead. The ImageViewcan still send events to an OnClickListenerand won't take over the focus.

OnClickListener为每个视图设置 an 的一种替代方法是不使用ImageButton-ImageView而是使用 an 。该ImageView仍可事件发送到OnClickListener并不会接管的焦点。

回答by meizilp

set your ImageButton's attribute:

设置你ImageButton的属性:

android:focusable="false"

Because AbsListView.onTouchEventcheck child.hasFocusable().

因为AbsListView.onTouchEvent检查child.hasFocusable()

回答by Pavel Dudka

As an alternative solution which worked for me you can try to extend your adapter from BaseAdapter(iso implementing ListAdapterinterface)

作为对我有用的替代解决方案,您可以尝试从BaseAdapter(iso 实现ListAdapter接口)扩展您的适配器

回答by JulianSymes

I've tested the following solution on SDK levels 8 and 16.

我已经在 SDK 级别 8 和 16 上测试了以下解决方案。

In getView()

getView()

setFocusable(false);
setClickable(false);

rather than setting them true in the Adapter's getView()does what I think the original question wanted, and means that an OnItemClickListenergets called, provided that an OnClickListeneris not set in getView().

而不是在 Adapter's 中将它们设置为 truegetView()做我认为最初的问题想要的,并且意味着OnItemClickListener调用an ,前提是 anOnClickListener未在getView().

I'm assuming that anything you can do in an View's OnClickListeneryou can do just as easily in a ListView's OnItemClickListener. (setOnClickListeneron a View implicitly sets the view to be clickable, which prevents the ListView's corresponding OnItemClickListenergetting called, apparently.)

我假设任何事情,你可以在一个视图做的OnClickListener,你可以很容易地做在ListViewOnItemClickListener。(setOnClickListener在 View 上隐式地将视图设置为可点击,这显然可以防止ListView的相应OnItemClickListener被调用。)

The behaviour is as one would expect, in terms of the ImageButton's visual state when the item is pressed or rolled over.

ImageButton当项目被按下或滚动时,行为正如人们所期望的那样,就的视觉状态而言。

The solution is a slight illusion, in that it is the list item that's being pressed not the ImageButtonitself, so if the button doesn't occupy whole list item, clicking somewhere else in the item will still make the button's drawable state reflect the click. Same for focus. That might be a price worth paying.

解决方案是一个轻微的错觉,因为它是被按下的列表项而不是它ImageButton本身,所以如果按钮没有占据整个列表项,点击项目中的其他地方仍然会使按钮的可绘制状态反映点击。对焦也是一样。这可能是一个值得付出的代价。

回答by silentkratos

best way to do is this:

最好的方法是这样的:

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

set these properties for that Imagebuttonand try. I

为此设置这些属性Imagebutton并尝试。一世

回答by Kapil Raju

This will definitely work. Add this to the layout definition.

这肯定会奏效。将此添加到布局定义中。

android:descendantFocusability="blocksDescendants" 

Found the solution here

这里找到了解决方案

回答by newb

Put This code ImageView nextpage= (ImageView)findViewById(R.id.btnEdit);instead of ImageButton. now the list item is active

把这个代码 ImageView nextpage= (ImageView)findViewById(R.id.btnEdit);而不是ImageButton. 现在列表项处于活动状态

回答by Lukas Novak

I have sub-classed ImageButtonand setFocusable="false"in layout definition didn't work for me. It solved calling setFocusable(false)in constructor of subclass.

我有子类ImageButtonsetFocusable="false"布局定义对我不起作用。它解决了调用setFocusable(false)子类的构造函数。