Search code examples
androidandroid-custom-viewviewgroup

Selector is applying change to all views instead of clicked view


I'm working on adding an onClick highlight to a view in a drag and drop grid in Android. The project is on GitHub (the dev branch contains the code that is using the selector).

The problem I am having is in the adapter when I set the onClick selector as a view's background, when I click a view, instead of changing the background for just that view the selector changes the backgrounds for all the views.

I'm pretty sure this is a result of how the clicks are being handled in onTouch in the ViewGroup (below). I'm not sure if I should I be returning true/false differently or triggering the onClick listener differently to prevent all of the views from being highlighted.

Any ideas/help is appreciated, thanks.

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        touchDown(event);
        break;
    case MotionEvent.ACTION_MOVE:
        touchMove(event);
        break;
    case MotionEvent.ACTION_UP:
        touchUp(event);
        break;
    }
    if (aViewIsDragged())
        return true;
    return false;
}

private void touchUp(MotionEvent event) {
    if(dragged == -1) {
        if(onClickListener != null) {
            View clickedView = getChildAt(getTargetAtCoor((int) event.getX(), (int) event.getY()));
            if(clickedView != null)
                onClickListener.onClick(clickedView);
        }
    } else {
        manageChildrenReordering();
        hideDeleteView();
        cancelEdgeTimer();

        movingView = false;
        dragged = -1;
        lastTarget = -1;
        container.enableScroll();
        cancelAnimations();
    }
}

EDIT: After looking at the ViewGroup documentation android:addStatesFromChildren sounds like what I am looking for, but it doesn't seem to have any effect.

android:addStatesFromChildren

Sets whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol addStatesFromChildren.

UPDATE

I have found a way to solve this. The pull request is here


Solution

  • Edit

    Add layout.setClickable(true); to the view function (somewhere between lines 133 and 156) to make each item clickable. This is required for them to have their own select-able state. Without it, the grid is receiving the touch and that state gets passed down to each child. This breaks your onTouch functionality, but that would be a separate question.

    If you don't want to worry about changing the functionality of your onTouch, you could manually change the background of the selected item in the different touch functions instead of using a selector


    Original that missed the point:

    Maybe I'm missing something, but I would think that you could pass the View "v" from onTouch and use that as clickedView instead of trying to figure out which view it is based on location.