2

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

Luke
  • 1,069
  • 18
  • 28
  • any chance to see your onClickListener method? Maybe performClick might help? – AndroidPenguin Dec 13 '12 at 17:40
  • onClickListener is just a reference to the click listener (the activity in this case) and it's just creating a toast. I swapped onClickListener.onClick(clickedView) with clickedView.performClick() but the same thing happens and on top of it the click listener is not called – Luke Dec 13 '12 at 18:00

1 Answers1

2

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.

Cameron
  • 3,098
  • 1
  • 22
  • 40
  • Finding the view by location is there because it finds the child view at the location, the v from onTouch is not always a child and will cause clicks to be triggered when a user just touches whitespace. Using v from onTouch also has nothing to do with the problem I'm having in my question – Luke Dec 19 '12 at 17:43
  • Sorry, guess I didn't understand what you were asking. Where in your code is the selector? I did a quick look through, but didn't see anything – Cameron Dec 19 '12 at 18:12
  • It's in the dev branch, the selector is set as the background of the view [here](https://github.com/lkorth/PagedDragDropGrid/blob/dev/example/src/ca/laplanete/mobile/example/ExamplePagedDragDropGridAdapter.java#L153) and the selector itself is [here](https://github.com/lkorth/PagedDragDropGrid/blob/dev/example/res/drawable/list_selector_holo_light.xml) – Luke Dec 19 '12 at 18:27
  • Brilliant, fixes the selector perfectly. Any ideas on how to handle onTouch now? – Luke Dec 19 '12 at 19:24
  • @Luke You'll need to either add an onTouchListener to each item that calls the grid's listener (not sure how you would do that), or remove the setClickable line and selector, then make the touch functions change the background of the view. The second option sounds easier to me, but I don't have the time to test and I'm not sure the implications it would have on the usability of it being a library – Cameron Dec 19 '12 at 19:59