ListView: Prevent a view from recycling
Asked Answered
M

3

5

I have a ListView wich use recycled views. I'm trying to prevent a view from recycle. So I use setHasTransientState:

android.support.v4.view.ViewCompatJB.setHasTransientState(View view, boolean hasTransientState)

It works very well on Jellybean version but it doesn't do anything on Api < 16. Is there a way to make it work or there is different approach for pre Jellybean ?


I found out how to set a RecyclerListener like @Daniel Chow suggested.

listView.setRecyclerListener(new RecyclerListener() {
        @Override
        public void onMovedToScrapHeap(View view) {
            // Stop animation on this view
        }
});
Manaker answered 25/5, 2013 at 16:42 Comment(3)
why would you want to prevent recycling views?Shawntashawwal
I want to animate a view. You can see how it should work here graphics-geek.blogspot.it/2013/02/…Manaker
did you use the support library function : developer.android.com/reference/android/support/v4/view/…, boolean)Saccharase
S
4

For pre Jellybean, I think you can just use setRecyclerListener on ListView and when RecyclerListener#onMovedToScrapHeap(View view) is called, clear the animation on the view who has been recycled and directly do the final job which was supposed to be done when animation ends.

The code inside onMovedToScrapHeap(View view) depends on how you implement the animation, e.g. you can call View#clearAnimation() if you previously used View#startAnimation to start animation.

Silva answered 25/5, 2013 at 18:4 Comment(3)
Can you write some code on how to implemet RecyclerListener#onMovedToScrapHeap(View view)Manaker
@Manaker There are quite a few ways to start animation in Android, so the code inside RecyclerListener#onMovedToScrapHeap(View view) depends. If you use ViewPropertyAnimator then you can call ViewPropertyAnimator#cancel(), if you use View#startAnimation to start animation then you should call View#clearAnimation() to stop it. The view passed to RecyclerListener#onMovedToScrapHeap(View view) is actually the same as convertView in Adapter#getView(), so you can also use findViewById or ViewHolder to find a child view and manipulate it.Silva
Isn't it possible to just stop the animation (if exists) on the getView ?Also, what would happen to the view that wasn't got to be recycled? will it go into the scrap pool of views eventually?Saccharase
M
3

Use android.support.v4.view.ViewCompat.setHasTransientState(View view, boolean hasTransientState) instead of android.support.v4.view.ViewCompatJB.setHasTransientState(View view, boolean hasTransientState)

Mask answered 26/6, 2014 at 4:43 Comment(0)
A
1

Besides the animation problem Daniel talked about, another issue where knowing when your view is recycled has to do with memory management. If you are putting large, memory intensive bitmaps in your list items, it may be possible that you don't want your view recycled if its not likely to be re-used by other items. This hook gives you a chance to clear the bitmap you may have assigned to an ImageView. Hopefully, this is a rare problem.

Ahders answered 23/7, 2013 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.