Suppose I have a listview with 3 rows. If the user clicks a button in row 1, it expands a menu (it's just a linear layout that's shown/hidden). If they then click on an item in row 2, I'd like to be able to collapse the menu in row 1. Is this possible? If so, how?
You can always use getContext()
on the View
to get a reference to the containing Activity
, which you can cast to a ListActivity
(assuming you're using that) and call the usual methods to get the data at that position in the ListAdapter:
((ListActivity) v.getContext()).getListAdapter().getItem(1)
Then you can manipulate your data however you want to and call notifyDataSetChanged()
on your ListAdapter.
But it'd probably be a lot easier for you and your users to just use ExpandableListView
, which gives you expandable lists with predictable interactions your users already know. You can check the history activity in the Browser source code in AOSP for a real life example. And if it doesn't exactly meet your needs, you can always yank the code for ExpandableListView
itself from the Android source.
Save a reference to the View you would like to manipulate later. Probably wrap in a final
variable, then you will be easy to change.