什么静态和非静态viewholder在RecyclerView适配器之间的区别是什么? [英] What difference between static and non static viewholder in RecyclerView Adapter?

查看:1971
本文介绍了什么静态和非静态viewholder在RecyclerView适配器之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这种方法的优点(在我的课MyAdapter使用静态嵌套类扩展RecyclerView.Adapter):

What are the advantages of this approach (using static nested class in my class MyAdapter extends RecyclerView.Adapter):

static class MyVH extends RecyclerView.ViewHolder {...}

和这种方法(使用成员内部类):

And this approach (using member inner class):

  class MyVH extends RecyclerView.ViewHolder {...}

或者,它不会影响性能,这两种方法可以使用​​?

Or it doesn't affect performance and both approaches could be used?

推荐答案

这更是一个Java的问题不是一个Android的问题。建议使用静态的内部类,以避免内存泄漏,如果你将自己的情况下进行的类。你可以看看<一href="http://stackoverflow.com/questions/10864853/when-exactly-is-it-leak-safe-to-use-anonymous-inner-classes">this真棒后,解释了内存泄漏的内部类。

It is more a java question than an Android question. It is recommended to use static for inner classes to avoid memory leaks if you will take their instances out of the class. You can have a look at this awesome post that explains the memory leaks on inner classes.

基本上,NYX说:

  • 如果您声明viewholder为静态,你可以在其他适配器重复使用它。反正,我不建议这样做,创建一个新的分离类,并从多个地方使用它,但它更有意义。一类为一个目的。
  • 在视图持有人的情况下,这类将只用于在适配器内部,它们的实例不应该仅仅根据定义转到片段或活性或其他地方。这是指具有它静止的或非静止,鉴于人的情况下,也是一样的。

回答你的表现的问题,你可以看看<一href="http://stackoverflow.com/questions/20380600/gc-performance-hit-for-inner-class-vs-static-nested-class">this回答。静态人会采取更少的内存比另一个,但同样,我们所谈论的回收,将回收的实例,所以内存的影响是没有问题的。

Answering your performance question, you can have a look at this answer. The static one will take less memory than the other one, but again, we are talking about recyclers which will recycle the instances, so the memory impact is not a problem.

这篇关于什么静态和非静态viewholder在RecyclerView适配器之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆