目前很多开发者使用依赖注入库来完成View的绑定工作,也有很多issue询问在BasePopup如何使用。因此本篇将会详细阐述如何在BasePopup中使用依赖注入库。
通常来说,我们在setContentView()之后就开始进行findView操作,而在ContentView被inflate后,就是我们初始化依赖注入库的最好的时间。
BasePopup提供了onViewCreated(@NonNull View contentView)
回调,您可以在您的BasePopup中override该方法,并在里面完成初始化操作。
由于很多库我都没用过,在这里我们十分欢迎大家补充一些依赖注入的使用例子。
ButterKnife
public class DemoPopup extends BasePopupWindow {
@BindView(R.id.tv_desc)
TextView mTvDesc;
public DemoPopup(Context context) {
super(context);
setContentView(R.layout.popup_demo);
}
@Override
public void onViewCreated(View contentView) {
ButterKnife.bind(this, contentView);
}
}
ViewBinding
public class DemoPopup extends BasePopupWindow {
PopupDemoBinding mBinding;
public DemoPopup(Context context) {
super(context);
setContentView(R.layout.popup_demo);
// do with binding
// mBinding.xxx.xxxx
}
@Override
public void onViewCreated(View contentView) {
mBinding = PopupDemoBinding.bind(contentView);
// do with binding
// mBinding.xxx.xxxx
}
}