我应该在哪里取消绑定 ButterKnife 8.x.x 在 ViewHolder 中? [英] Where should I unbind ButterKnife 8.x.x in a ViewHolder?

查看:31
本文介绍了我应该在哪里取消绑定 ButterKnife 8.x.x 在 ViewHolder 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 ButterKnife 注释的 RecycleView.ViewHolder 类.

我的代码也应该在这个 ViewHolder 类中 unbind() 吗?

公共类 AView 扩展 RecyclerView.ViewHolder{@BindView(R.id.a_text_view) TextView aText;公共 AView(最终视图视图){超级(查看);ButterKnife.bind(this, view);//它返回一个 Unbinder,但我应该在哪里调用它的 unbind()?}}

文档(http://jakewharton.github.io/butterknife/)不说话关于这个问题.

解决方案

根据 Butterknife 的作者 Jake Wharton 的说法,只有 Fragments 需要 unbind().在问题跟踪器上查看此评论:

https://github.com/JakeWharton/butterknife/issues/879

<块引用>

Q:在RecyclerView中,如何解绑ViewHolder?

答:你不需要.在onDestroyView()中只需要Fragments.

原因是

<块引用>

[ViewHolders] 的生命周期不会超过相关视图.Fragment 可以.

换句话说,因为一个Fragment在它的Views被销毁后可能会继续存在,所以你需要调用.unbind()一个 Fragment 来释放对 Views 的引用(并允许回收相关的内存).

使用ViewHolder,持有者的生命周期与它持有的Views 相同.换句话说,ViewHolder 和它的 Views 是同时销毁的,所以你不需要手动清除一个到另一个的挥之不去的引用.>

I have a RecycleView.ViewHolder class which use ButterKnife annotations.

Should my code unbind() in this ViewHolder class too?

public class AView extends RecyclerView.ViewHolder
{
    @BindView(R.id.a_text_view) TextView aText;

    public AView(final View view)
    {
        super(view);
        ButterKnife.bind(this, view); // It returns an Unbinder, but where should I call its unbind()?
    }
}

The docs (http://jakewharton.github.io/butterknife/) does not talk about this issue.

解决方案

According to Jake Wharton, author of Butterknife, unbind() is only required for Fragments. See this comment on the issue tracker:

https://github.com/JakeWharton/butterknife/issues/879

Q: In the RecyclerView, how do we unbind the ViewHolder?

A: You don't need to. Only Fragments need to in onDestroyView().

The reason being that

[ViewHolders] don't outlive the associated view. A Fragment does.

In other words, because a Fragment may continue to exist after its Views are destroyed, you need to call .unbind() from a Fragment to release the reference to the Views (and allow the associated memory to be reclaimed).

With a ViewHolder, the lifecycle of the holder is the same as the Views it holds. In other words, the ViewHolder and its Views are destroyed at the same time, so there's never a lingering reference from one to the other that you need to manually clear.

这篇关于我应该在哪里取消绑定 ButterKnife 8.x.x 在 ViewHolder 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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