Android list tap event delegation
I have a list that uses a database to display some data. There are several operations that the user can perform on each list item and instead of implementing a context menu where the user uses a long press to bring up a list of possible operations I would like to add buttons to each item so the user can just tap the button and perform the operation. The list can be potentially large and attaching a listener to each button for each list item is overkill so I would like to do what Javascript programmers do with event bubbling, i.e. attach a single handler to a top level element like the entire list and let the click events bubble up to it. How开发者_JS百科 would I go about doing this?
View.OnClickListener.onClick() does not bubble, so the solution you propose would not work.
OTOH, View.OnTouchListener.onTouch() does bubble so this could possibly be used, but it would require you to manually handle MotionEvent's down/up to detect a click.
Besides, if you create a lot of Buttons, than are you sure that adding onCLick handlers would be a lot of overhead, especially since you can register the same method for all of them.
What you are trying to do sounds like a premature optimization. Be sure there is real overhead affecting your users, before you try to deal with it.
精彩评论