Android 无法单击带有图像按钮的列表视图行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11428303/
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-20 07:23:20  来源:igfitidea点击:

can't click on listview row with imagebutton

androidlistviewimagebutton

提问by yital9

I have trouble with listview. its items (rows) have imagebutton. imagebutton has "android:onClick" so this onclick event is working, but click on row doesnt work. if i remove imagebutton from row item, click on row works (listview has correct onclick listner). How can i fix it ? i need onclick event when user click on imagebutton and standart click event, when user select row (not click the imagebutton but click the row)

我在使用列表视图时遇到问题。它的项目(行)有图像按钮。imagebutton 有 "android:onClick" 所以这个 onclick 事件有效,但点击行不起作用。如果我从行项目中删除图像按钮,单击行有效(listview 具有正确的 onclick 监听器)。我该如何解决?当用户单击图像按钮和标准单击事件时,我需要 onclick 事件,当用户选择行时(不是单击图像按钮而是单击该行)

my listview:

我的列表视图:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/restaurants_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@color/list_devider"
        android:dividerHeight="1dp"
        android:cacheColorHint="@color/list_background" /> 

回答by jiasli

Unfortunately,

很遗憾,

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

doesn't work for ImageButton.

不适用于ImageButton.

I finally found the solution here. In your layout xml for those items, add

我终于在这里找到了解决方案。在这些项目的布局 xml 中,添加

android:descendantFocusability="blocksDescendants" 

to the root view.

到根视图。

It works perfectly for a ListViewthat has ImageButtons. According to official reference, blocksDescendantsmeans that the ViewGroupwill block its descendants from receiving focus.

它非常适合ListView具有ImageButtons 的 a。根据官方参考blocksDescendants意味着ViewGroup将阻止其后代接收焦点。

回答by Angelo

You can use a custom adapter for your listView (if you haven't already). And there, in the getView(int position, View inView, ViewGroup parent)method of the adapter do something like this:

您可以为您的 listView 使用自定义适配器(如果您还没有)。在那里,在getView(int position, View inView, ViewGroup parent)适配器的方法中做这样的事情:

@Override
public View getView(int position, View inView, ViewGroup parent) {

    View v = inView;
    ViewHolder viewHolder; //Use a viewholder for sufficent use of the listview

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) adaptersContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.image = (ImageView) v.findViewById(R.id.ImageView);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

        .....

    viewHolder.image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            //Click on imageView
        }i
    });

    v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            //Click on listView row
        }
    });

        .....

    return (v);
}

See hereif you need help creating your custom adapter.

如果您创建自定义适配器方面需要帮助,请参阅此处

回答by Mohsin Naeem

If a row of listView have any clickable element like Button, Image..etc..then onItemClickwill not work. So you need to write the click listener in getViewof your list adapter.

如果ListView控件的一排有一个像任何可点击元素ButtonImage..etc..thenonItemClick将无法正常工作。所以你需要在getView你的列表适配器中编写点击监听器。

For more read this.

欲了解更多信息,请阅读此内容

回答by Yogesh Somani

Set these properties for your button:

为您的按钮设置这些属性:

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

Or you can set it dynamically in your adapter class:

或者您可以在您的适配器类中动态设置它:

        yourButton.setFocusable(false);
    yourButton.setFocusableInTouchMode(false);

And make sure that you set the choice mode as single for the listview:

并确保您将列表视图的选择模式设置为单一:

       listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

回答by Giurgiu Ovidiu

In my case, android:descendantFocusability="blocksDescendants"for main layer did not work, neither in the ListView. I also tried android:focusable="false" android:focusableInTouchMode="false"which I heard that it is working for Buttons, but I had ImageButton so it didn't.

就我而言,android:descendantFocusability="blocksDescendants"对于主层不起作用,在 ListView 中也不起作用。我也试过android:focusable="false" android:focusableInTouchMode="false"我听说它适用于按钮,但我有 ImageButton 所以它没有。

But setting the properties of the button in the CS file of the Layout worked.

但是在布局的 CS 文件中设置按钮的属性有效。

var imageButton = view.FindViewById<ImageButton>(Resource.Id.imageButton1);
imageButton.Focusable = false;
imageButton.FocusableInTouchMode = false;
imageButton.Clickable = true;

回答by Maarten

If a row has multiple clickable elements, onItemClick() will not work. You will need to set the OnClickListenerin the getView()method. Store the listeners the the View's tag so that they can be recycled, add methods to your listeners so they can be specialized for different rows.

如果一行有多个可点击元素,则 onItemClick() 将不起作用。您需要OnClickListenergetView()方法中设置。将侦听器存储在 View 的标签中,以便它们可以被回收,将方法添加到您的侦听器中,以便它们可以专门用于不同的行。

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        RowClickListeners listeners = (RowClickListeners) view.getTag();
        if (listeners == null) {
            listeners = new RowClickListeners();
        }

        // Row click listener:
        RowClickListener onClickListener = listeners.rowClickListener;
        if (onClickListener == null) {
            onClickListener = new RowClickListener();
            listeners.rowClickListener = onClickListener;
        }
                    onClickListener.setToPosition(pos);
        view.setOnClickListener(onClickListener);

        // Overflow listener:
        View btn = view.findViewById(R.id.ic_row_btn);
        ButtonListener btnListener = listeners.buttonClickListener;
        if (rowListener == null) {
            btnListener = new ButtonListener(activity);
            listeners.rowClickListener = btnListener;
        }
                    btnListener.setToPosition(pos);
        btnListener.setCollection(collectionId);
        btn.setOnClickListener(btnListener);
    }


    public static class RowClickListeners {
        public RowClickListener rowClickListener;
        public ButtonListener buttonClickListener;
    }

回答by SteelBytes

no single answer above worked for me, but a combination did.

上面没有一个单一的答案对我有用,但一个组合对我有用。

I now set android:descendantFocusability="blocksDescendants"on the ListView and android:focusable="false" android:focusableInTouchMode="false"on the ImageButtons in the XML ANDin Java I also set descendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS)on the ListView and focusable(false), focusableInTouchMode(false), clickable(true)on the ImageButtons.

我现在设置android:descendantFocusability="blocksDescendants"的ListView和android:focusable="false" android:focusableInTouchMode="false"在XML中ImageButtons在Java中我还设置descendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS)的ListView和focusable(false), focusableInTouchMode(false), clickable(true)上ImageButtons。