避免内部列表视图ImageView的悬停状态,如果列表项为pressed [英] Avoid ImageView hover state inside Listview if List Item is pressed

查看:173
本文介绍了避免内部列表视图ImageView的悬停状态,如果列表项为pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ImageView 列表视图位于最左侧的position.This 的ImageView 的ListView 拥有所有州(pressed,选择,有重点,正常的)。如果我点击的ListView 而不是的ImageView 然后在一些设备的ImageView的悬停状态(选择,有重点,pressed)被调用。
所以我想知道如何避免这一点。

I have a ImageView inside listview in left most position.This imageView and listView has all states (pressed ,selected,focused,normal).If I click ListView but not ImageView then in some devices the hover state(selected,focused,pressed) of ImageView is called. So I want to know how to avoid this.

推荐答案

下面是解决这一问题的步骤。创建一个名为类 NoParent pressImageView 通过以下code:

Here are the steps to resolve this issue. Create a class called NoParentPressImageView with the following code:

public class NoParentPressImageView extends ImageView {

    public NoParentPressImageView(Context context) {
        this(context, null);
        this.setFocusable(false);
    }

    public NoParentPressImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setFocusable(false);
    }

    @Override
    public void setPressed(boolean pressed) {
        // If the parent is pressed, do not set to pressed.
        if (pressed && ((View) getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }
}

请注意, setFocusable(假)中的构造方法与添加的android一样的效果:可聚焦=FALSE XML 布局文件。这使得的ListView 行点击。在上pssed $ P $ 重写方法解决了你在这里提一下,即在的ListView preSS事件问题(父)到荡漾的ImageView (子)。

Note that the setFocusable(false) in the constructor has the same effect as adding android:focusable="false" to the xml layout document. This allows the ListView rows to be clickable. The onPressed override method solves the problem that you mention here, namely press events in the ListView (parent) rippling through to the ImageView (child).

一旦你定义了这个类,可以替换的ImageView XML 布局 com.MYCOMPANY.MYAPP.NoParent pressImageView 。编译并运行,而的ListView 现在应该像您期望的,用的ImageView pressed事件仅是表现触发当你真正点击图片上,而不是当你的行中点击其他地方。

Once you have this class defined, you can replace ImageView in your xml layout with com.MYCOMPANY.MYAPP.NoParentPressImageView. Compile and run, and the ListView should now behave as you expect, with the ImageView pressed events only being triggered when you actually click on the image and not when you click elsewhere in the row.

这篇关于避免内部列表视图ImageView的悬停状态,如果列表项为pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆