在ViewHolder中,我应该在何处解除ButterKnife 8.x.x的绑定? [英] Where should I unbind ButterKnife 8.x.x in a ViewHolder?

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

问题描述

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

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

我的代码也应该在此ViewHolder类中取消绑定吗?

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()?
    }
}

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

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

推荐答案

根据Butterknife的作者Jake Wharton的说法,unbind()仅对于Fragments才是必需的.请在问题跟踪器上查看此评论:

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

问:在RecyclerView中,我们如何解除ViewHolder的绑定?

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

A:不需要.只有Fragments才需要进入onDestroyView().

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

原因是

[ViewHolders]不会超过关联的视图. Fragment可以.

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

换句话说,由于Fragment在其Views被销毁后可能继续存在,因此您需要从Fragment调用.unbind()释放对Views的引用(并允许关联要回收的内存).

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).

对于ViewHolder,支架的生命周期与其所保持的Views相同.换句话说,ViewHolder及其Views会同时被销毁,因此您无需手动清除一个到另一个的缠绵引用.

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.

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

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