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