Android:如何将单击事件传播到 LinearLayout 子项并更改其可绘制对象

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

Android: How to propagate click event to LinearLayout childs and change their drawable

androidonclickandroid-linearlayout

提问by Bhiefer

I would like to create a linear layout which would behave similarly to ImageButton.

我想创建一个线性布局,其行为类似于 ImageButton。

<LinearLayout
    android:id="@+id/container"
    style="?WidgetHomeIconContainer">            

    <ImageView
        android:id="@+id/icon"
        style="?WidgetHomeIcon" />

    <TextView
        android:id="@+id/title"
        style="?WidgetHomeLabel"             
        android:text="@string/title"
        android:textAppearance="?attr/TextHomeLabel" />
</LinearLayout>

In styles of ImageView, TextView and LinearLayout, I set a selectors for all states.

在 ImageView、TextView 和 LinearLayout 样式中,我为所有状态设置了一个选择器。

Now:

现在:

  • when I click on ImageView (I tried it also with ImageButton) - it behaves correctly and the image is changed according the selector xml.
  • when I click on LinearLayout - the linear layout is clicked, but the the ImageView and TextView don't change it's drawable/appearance
  • 当我点击 ImageView(我也用 ImageButton 尝试过)时 - 它的行为正确并且图像根据选择器 xml 更改。
  • 当我点击 LinearLayout 时 - 点击了线性布局,但 ImageView 和 TextView 不会改变它的可绘制/外观

So I would like to do the following. When I click on parent LinearLayout, I need to change all it's childs to pressed state.

所以我想做以下事情。当我单击父 LinearLayout 时,我需要将所有子项更改为按下状态。

I tried to add following code to LinearLayout onClickListener to propagate the click:

我尝试将以下代码添加到 LinearLayout onClickListener 以传播点击:

@Override
public void onClick(View v)
{
    LinearLayout l = (LinearLayout) v;
    for(int i = 0; i < l.getChildCount(); i++)
    {
        l.getChildAt(i).setClickable(true);
        l.getChildAt(i).performClick();
    }
}

But it still reamins the same. Thank you very much for any help.

但它仍然是一样的。非常感谢您的帮助。

回答by Evusas

Not only make for every child:

不仅让每个孩子:

android:duplicateParentState="true"

But also additionally:

但另外:

android:clickable="false"  

This will prevent unexpected behaviour (or solution simply not working) if clickable child viewsare used.

如果使用可点击的子视图,这将防止意外行为(或解决方案根本不起作用)。

SO Source

SO 来源

回答by IgniteCoders

In my case, no one of the other solutions works!

就我而言,其他任何解决方案都无效!

I finally had to use OnTouchListeneras explained here, capturing the event when the user clicks in the parent view, and removing all childs OnClickListener.

我终于不得不OnTouchListener按照这里的解释使用,当用户在父视图中单击时捕获事件,并删除所有子视图OnClickListener

So the idea is, delegate the click behavior to the parent, and notify the childthat is really clicked, if you want to propagate the event. ??That's what we are looking for!!

所以这个想法是,将点击行为委托给父级,并通知真正被点击的子级,如果你想传播事件。??这就是我们要找的!!

Then, we need to check which child has been clicked. You can find a reference hereto know how it′s done. But the idea is basiclly getting the area of the child, and asking for who contains the clicked coordinates, to perform his action (or not).

然后,我们需要检查哪个孩子被点击了。您可以在此处找到参考资料以了解它是如何完成的。但这个想法基本上是获取孩子的区域,并询问谁包含点击的坐标,以执行他的动作(或不执行

回答by Bernd

After having the same problem some months later, I found this solution:

几个月后遇到同样的问题后,我找到了这个解决方案:

private void setOnClickListeners() {
    super.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            onClick(v);
        }
    });
    for (int index = 0; index < super.getChildCount(); index++) {
        View view = super.getChildAt(index);
        view.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onClick(v);
            }
        });
    }
}

protected void onClick(View v) {
    // something to do here...
}

回答by aldok

At first, my child view failed to get click from parent. After investigating, what I need to do to make it work are:

起初,我的子视图无法从父视图获得点击。经过调查,我需要做的是让它工作:

  1. remove click listener on child view
  2. adding click listener on parent view
  1. 删除子视图上的单击侦听器
  2. 在父视图上添加单击侦听器

So, I don't need to add these on every children.

所以,我不需要在每个孩子身上都添加这些。

android:duplicateParentState="true"
android:clickable="false"

I only add duplicateParentStateto one of my child view.

我只添加duplicateParentState到我的子视图之一。

My child view is now listening to parent click event.

我的子视图现在正在侦听父单击事件。