splitwise / TokenAutoComplete

Gmail style MultiAutoCompleteTextView for Android
Apache License 2.0
1.3k stars 383 forks source link

Would it be possible to implement TokenClickStyle.Custom? #250

Closed a-mehta closed 8 years ago

a-mehta commented 8 years ago

The desired behaviour is to click a token, have it toggle states (selected), and then have an X appear over the contact image. Clicking on anything else but the X will cause it to become unselected. Clicking on the X will delete it.

TokenClickStyle.SelectDeselect is almost what I need, but not quite. I need to be able to add a touch/click event to a subview.

TLDR; I want to select/deselect tokens, but be able to delete them if a specific subview/hit area is clicked. Is this possible @mgod ?

mgod commented 8 years ago

Not really. You can override onClick in a subview of TokenImageSpan and use that in overriding buildSpanForObject. You'll then need to get the raw click event from onTouchEvent and figure out the position of the click in the span (I'm not sure how to do this part).

VeneetReddy commented 8 years ago

@a-mehta were you able to achieve this?

a-mehta commented 8 years ago

@VeneetReddy Not yet.

My first attempt was to subclass the TokenImageSpan and override the onClick or expose the underlying view, but the class is protected so I can't do that.

My second attempt was to set click listeners in buildSpanForObject, but the click was being entirely absorbed by the TokenClickStyle.

Another solution was to override the onTouchEvent and handle the touch event based on the event's x and y, but I had no success with this. For some odd reason, it always thought I was trying to click the first view.

mgod commented 8 years ago

@VeneetReddy @a-mehta the underlying problem that you're running into is that the TokenCompleteTextView doesn't actually use the views in the layout. What happens is the TokenCompleteTextView draws the token view to a buffer, then attaches the resulting drawing to a text span in the EditText. To figure out what part of a token was clicked, you'll need to figure out the x and y coordinate of the touch event in the TokenCompleteTextView, then simulate text layout of the tokens currently in the view to figure out what part of a token is clicked on. The EditText click detection code doesn't have great ways to do this, so you're probably going to wind up building a separate text layout engine to figure this out.

I think you'll probably have an easier time not using this library if you really need this behavior. As the person who wrote it, if it were a requirement that the tokens be able to handle clicks on subviews separately, I would probably abandon this library and write a new version of this that just uses some kind of non text based layout system to put the views on screen and just made the last view in my layout the edit text for searching. I haven't looked, but it seems like there should be some ViewGroup implementation for laying out arbitrary width views in rows somewhere on github.