Skip to content Skip to sidebar Skip to footer

Avoid Imageview Hover State Inside Listview If List Item Is 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

Solution 1:

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

publicclassNoParentPressImageViewextendsImageView {

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

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

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

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).

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.

Solution 2:

Try adding android:focusable="false" to the ImageView in your layout file.

Post a Comment for "Avoid Imageview Hover State Inside Listview If List Item Is Pressed"