简体   繁体   English

父级和子级视图的单击事件

[英]On Click event for Parent and Child view

I have a RelativeLayout and an ImageView . 我有一个RelativeLayout和一个ImageView
The layout is given below - 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" >

<ImageView
    android:id="@+id/ivIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher" />

</RelativeLayout>

The Activity code is - Activity代码是-

public class MainActivity extends Activity implements OnClickListener{

private RelativeLayout rlMain;
private ImageView ivIcon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rlMain = (RelativeLayout) findViewById(R.id.rlMain);
    ivIcon = (ImageView) findViewById(R.id.ivIcon);
    rlMain.setOnClickListener(this);
    ivIcon.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.rlMain:
        Toast.makeText(this, "Relative Layout clicked", Toast.LENGTH_SHORT).show();
        break;

    case R.id.ivIcon:
        Toast.makeText(this, "Image View clicked", Toast.LENGTH_SHORT).show();
        break;
    }

}

}

I have applied the on click listener for both the RelativeLayout (parent) and the ImageView (child). 我已经为RelativeLayout (父级)和ImageView (子级)应用了点击监听器。
While clicking on the relative layout - it is handled by the relative layout click handler.(This seems to be correct). 在单击相对布局时-由相对布局单击处理程序处理。(这似乎是正确的)。

While clicking on the Imagview - It is handled by the imageview. 单击Imagview时-由imageview处理。 (Here is the confusion). (这是混乱)。

Should the click on the imageview get handled by both the parent(relative layout) and child (imageview)? imageview上的点击是否应该同时由父级(相对布局)和子级(imageview)处理? What is the logic if only the child view is handling the click? 如果只有子视图正在处理点击,逻辑是什么?

A clickEvent will be delivered to the lowest child element in the layout hierarchy. clickEvent将传递到布局层次结构中的最低子元素。 If this element does not have an onClick behaviour it will pass the event up to its parent until the event gets handled. 如果此元素没有onClick行为,它将将该事件传递到其父项,直到事件得到处理为止。

Therefore you can treat the LinearLayout as one single block for your onClick behaviour. 因此,您可以将LinearLayout视为onClick行为的一个整体。 If you create another clickable element inside the layout be sure to make it big enough to reduce the chance of the user missing the correct item. 如果在布局内创建另一个可单击元素,请确保使其足够大以减少用户错过正确项目的机会。

Source: Does making parent clickable make all child element clickable as well? 来源: 使父级可点击是否也使所有子元素也可点击?

Normally in cases where the image to be clicked is very small in dimension the imageView is kept inside a layout which is given some padding, then the click of the layout is handled. 通常,在要单击的图像尺寸很小的情况下,imageView会保留在具有一些填充的布局内,然后处理该布局的单击。 This gives enough area on screen for the user to click on 屏幕上有足够的区域供用户单击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM