I need to find out the pixel position of one element in a list that's been displayed using a ListView
. It seems like I should get one of the TextView's and then use getTop()
, but I can't figure out how to get a child view of a ListView
.
Update: The children of the ViewGroup
do not correspond 1-to-1 with the items in the list, for a ListView
. Instead, the ViewGroup
's children correspond to only those views that are visible right now. So getChildAt()
operates on an index that's internal to the ViewGroup
and doesn't necessarily have anything to do with the position in the list that the ListView
uses.
Basically, create two drawables - one that is transparent, and another that is the desired color. Request focus at the clicked position (int position as defined) and change the color of said row. Then walk through the parent
listview
, and change all other rows accordingly. This accounts for when a user clicks on thelistview
multiple times. This is done with a custom layout for each row in thelistview
. (Very simple, just a new layout file with atextview
- do not set focusable or clickable!) No custom adapter required - use array adapterSee: Android ListView: get data index of visible item and combine with part of Feet's answer above, can give you something like:
The benefit is that you aren't iterating over the ListView's children, which could take a performance hit.
This code is easier to use:
A quick search of the docs for the ListView class has turned up getChildCount() and getChildAt() methods inherited from ViewGroup. Can you iterate through them using these? I'm not sure but it's worth a try.
Found it here
This assumes you know the position of the element in the ListView :
Then you should be able to call getLeft() and getTop() to determine the elements on screen position.