在ViewHolder中,我应该在何处解除ButterKnife 8.x.x的绑定? [英] Where should I unbind ButterKnife 8.x.x in a ViewHolder?
问题描述
我有一个使用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 theViewHolder
?
A:不需要.只有Fragments
才需要进入onDestroyView()
.
A: You don't need to. Only Fragments
need to in onDestroyView()
.
原因是
[
ViewHolders
]不会超过关联的视图.Fragment
可以.
[
ViewHolders
] don't outlive the associated view. AFragment
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屋!