I have a ListView with a BaseAdapter. I want to update some items on the list very often without refreshing the content. In order to achieve this I'm using the Observer pattern. I have an Observable object and every item in the list is an observer.
public View getView(final int position, View convertView, final ViewGroup parent) {
final ItemViewHolder holder;
final Item item = (Item) getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, parent, false);
/* ADD VIEWS TO HOLDER */
holder = new ItemViewHolder();
convertView.setTag(holder);
ItemObserver observer = new ItemObserver(holder, context);
mapObservers.put(holder, observer);
observable.addObserver(observer);
} else {
holder = (ItemViewHolder) convertView.getTag();
}
//Set values into view etc...
return convertView;
}
This works well but the issue is: how do I know the observer is not longer needed?. I want to detect when a given view is destroyed in order to get the observer from the Map and delete it from the Observable object. Is there any listener that is called when the view is destroyed?
I tried AbsListView.RecyclerListener.onMovedToScrapHeap(View view)
but it seems to be called when the View is recycled.
Thank you!
According to this offical issue, There is an issue with the onViewDetachFromWindow
BUT
onDetachedFromWindow() will not necessarily be called in this situation. ListView children are recycled and reused, and ListView avoids unnecessary onDetachedFromWindow() whenever possible.
However, onDetachedFromWindow() should be called for all views created by the adapter when the Activity is destroyed or when ListView is detached from the window.
So you can't expect the listview views to be destroyed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With