RecyclerView Adapter 中的静态和非静态视图有什么区别? [英] What difference between static and non static viewholder in RecyclerView Adapter?

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

问题描述

这种方法的优点是什么(在我的类 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?

推荐答案

与其说是 Android 问题,不如说是 Java 问题.如果您将它们的实例从类中取出,建议对内部类使用静态以避免内存泄漏.你可以看看这个 解释内部类内存泄漏的精彩帖子.

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 所说的:

Basically what nyx says:

  • 如果您将 viewholder 声明为静态,则可以在其他适配器中重用它.无论如何,我不建议这样做,创建一个新的分离类并从多个地方使用它,它确实更有意义.一堂课,一堂课.
  • 在视图持有者的情况下,这些类将仅在适配器内部使用,它们的实例不应仅根据定义进入片段或活动或其他地方.这意味着对于视图持有者来说,它是静态的还是非静态的,是一样的.

回答你的性能问题,可以看看这个 答案.静态的将比另一个占用更少的内存,但同样,我们正在谈论回收器,它会回收实例,因此内存影响不是问题.

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.

这篇关于RecyclerView Adapter 中的静态和非静态视图有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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