How to get getSelectedView() to work in GridView?
I have a GridView
in a layout. It is populated with Foo
views by the activity using a extended BaseAdapter
.
When I select an item in this grid it gets orange tinted (thus selected). That's nice. But I want to access this selection from outside the GridVie开发者_开发百科w and it's parent activity: from within another View
higher in the layout hierarchy. I therefor call upon gridView.getSelectedItem()
. However it always returns null
.
How could I get this to work?
"Selection" doesn't mean the same thing in AndroidOS as it does in other UIs. In particular, there isn't any "selected item" when you're in touch mode. You probably need to use a click listener instead of relying on there being a "selected item". See this article for details.
You can use the following to get the view:
View childView = gridView.getChildAt(position - gridView.getFirstVisiblePosition());
精彩评论