简体   繁体   English

ListView:防止回收视图

[英]ListView: Prevent a view from recycling

I have a ListView wich use recycled views. 我有一个使用回收视图的ListView。 I'm trying to prevent a view from recycle. 我试图阻止视图回收。 So I use setHasTransientState: 所以我使用setHasTransientState:

android.support.v4.view.ViewCompatJB.setHasTransientState(View view, boolean hasTransientState) 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 ? 它在Jellybean版本上工作得非常好,但它在Api <16上没有做任何事情。有没有办法让它工作或者有不同的方法用于预先豆豉?


I found out how to set a RecyclerListener like @Daniel Chow suggested. 我发现了如何设置像@Daniel Chow建议的RecyclerListener。

listView.setRecyclerListener(new RecyclerListener() {
        @Override
        public void onMovedToScrapHeap(View view) {
            // Stop animation on this view
        }
});

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. 对于Jellybean之前,我认为您可以在ListView上使用setRecyclerListener,并且当调用RecyclerListener#onMovedToScrapHeap(视图视图)时,清除已经回收的视图上的动画,并直接执行动画结束时应该完成的最终作业。

The code inside onMovedToScrapHeap(View view) depends on how you implement the animation, eg you can call View#clearAnimation() if you previously used View#startAnimation to start animation. onMovedToScrapHeap(视图视图)中的代码取决于您实现动画的方式,例如,如果您之前使用View#startAnimation来启动动画,则可以调用View#clearAnimation()。

使用android.support.v4.view.ViewCompat.setHasTransientState(View view,boolean hasTransientState)而不是android.support.v4.view.ViewCompatJB.setHasTransientState(View view,boolean hasTransientState)

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. 此挂钩使您有机会清除可能已分配给ImageView的位图。 Hopefully, this is a rare problem. 希望这是一个罕见的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM