Note that there are some explanatory texts on larger screens.

plurals
  1. POSelector is applying change to all views instead of clicked view
    primarykey
    data
    text
    <p>I'm working on adding an onClick highlight to a view in a drag and drop grid in Android. The project is on <a href="https://github.com/lkorth/PagedDragDropGrid" rel="nofollow">GitHub</a> (the dev branch contains the code that is using the selector).</p> <p>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.</p> <p>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.</p> <p>Any ideas/help is appreciated, thanks.</p> <pre><code>@Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action &amp; 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(); } } </code></pre> <p>EDIT: After looking at the ViewGroup documentation <a href="http://developer.android.com/reference/android/view/ViewGroup.html#attr_android%3aaddStatesFromChildren" rel="nofollow">android:addStatesFromChildren</a> sounds like what I am looking for, but it doesn't seem to have any effect.</p> <blockquote> <p>android:addStatesFromChildren</p> <p>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.</p> <p>Must be a boolean value, either "true" or "false".</p> <p>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.</p> <p>This corresponds to the global attribute resource symbol addStatesFromChildren.</p> </blockquote> <p><strong>UPDATE</strong></p> <p>I have found a way to solve this. The pull request is <a href="https://github.com/mrKlar/PagedDragDropGrid/pull/12" rel="nofollow">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload