I have a listView and want to expand each item when you press it. I have the following code to do so. I works by inflating a ViewStub.
It works fine. That is, when I press a list item it does indeed inflate. So far so good.
However, my list is long, and I noticed that when I scroll down the list other items have inflated as well. I'm not entirely sure why. And when I scroll up again the item I originally press has "deflated" and some other item has become inflated instead.
@override
public void onListItemClick(ListView l, View v, int position, long id) {
ListView list = getListView();
int start = list.getFirstVisiblePosition();
for (int i = start, j = list.getLastVisiblePosition(); i <= j; i++) {
Cursor item = (Cursor) list.getItemAtPosition(i);
if (id == item.getLong(item.getColumnIndex(ItemColumns._ID))) {
View view = list.getChildAt(i - start);
ViewStub stub = (ViewStub) view.findViewById(R.id.stub);
if (stub != null) {
stub.inflate();
}
}
}
}
Any help will be much appreciated!