目前很多开发者使用依赖注入库来完成View的绑定工作,也有很多issue询问在BasePopup如何使用。因此本篇将会详细阐述如何在BasePopup中使用依赖注入库。
通常来说,我们在setContentView()之后就开始进行findView操作,而在ContentView被inflate后,就是我们初始化依赖注入库的最好的时间。
BasePopup提供了onViewCreated(@NonNull View contentView)回调,您可以在您的BasePopup中override该方法,并在里面完成初始化操作。
由于很多库我都没用过,在这里我们十分欢迎大家补充一些依赖注入的使用例子。

ButterKnife

  1. public class DemoPopup extends BasePopupWindow {
  2. @BindView(R.id.tv_desc)
  3. TextView mTvDesc;
  4. public DemoPopup(Context context) {
  5. super(context);
  6. setContentView(R.layout.popup_demo);
  7. }
  8. @Override
  9. public void onViewCreated(View contentView) {
  10. ButterKnife.bind(this, contentView);
  11. }
  12. }

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
    }
}