封装dialog时一些不解的地方

一个点击事件失效的记录

下面是一个作为父类的dialog

js 复制代码
public abstract class BaseDialog <VDB extends ViewDataBinding> extends AppCompatDialog {

    protected VDB binding;

    public BaseDialog(@NonNull Context context) {
        super(context);
        binding = DataBindingUtil.inflate(
                getLayoutInflater(),
                getContentViewId(),
                null,
                false
        );
        setContentView(getContentViewId());
        processLogic();
    }


    protected abstract int getContentViewId();

    /**
    *处理业务逻辑
     * */
    protected abstract void processLogic();


}

这是他的一个子类

js 复制代码
public class GainDialog extends BaseDialog<DialogGainBinding> {
    public GainDialog(@NonNull Context context) {
        super(context);
        Log.d("dialog","22222");
       
    }
    
    @Override
    protected int getContentViewId() {
        return R.layout.dialog_gain;
    }

    @Override
    protected void processLogic() {
        binding.closeButton.setOnClickListener(v -> {
            dismiss();
        });
    }
    
}

可以看到在这个子类中,我们已经对closeButton设置了点击事件让其关闭,但是当我们运行之后,点击这个按钮发现并没有像我们预期之中一样关闭弹窗。 经过排查发现问题出现在父类的这一行代码

setContentView(getContentViewId());

如果把这行代码换成

setContentView(binding.getRoot());

点击事件就能成功响应了。

为什么会这样还没有仔细分析。除此之外,还有一个疑惑的的地方:就是当我们实例化GainDialog时用的是

GainDialog dialog = new GainDialog(MainActivity.this);

为什么还调用了父类里的构造方法

js 复制代码
public BaseDialog(@NonNull Context context) {
        super(context);
        binding = DataBindingUtil.inflate(
                getLayoutInflater(),
                getContentViewId(),
                null,
                false
        );
        setContentView(getContentViewId());
        processLogic();
    }
相关推荐
AH_HH17 分钟前
SmartCabinet:基于 Android 的智能储物柜管理系统技术解析
android·kotlin·存储柜
西部风情18 分钟前
聊聊并发、在线、TPS
android·java·数据库
2501_916008895 小时前
Web 前端开发常用工具推荐与团队实践分享
android·前端·ios·小程序·uni-app·iphone·webview
我科绝伦(Huanhuan Zhou)6 小时前
MySQL一键升级脚本(5.7-8.0)
android·mysql·adb
怪兽20147 小时前
Android View, SurfaceView, GLSurfaceView 的区别
android·面试
龚礼鹏8 小时前
android 图像显示框架二——流程分析
android
消失的旧时光-19438 小时前
kmp需要技能
android·设计模式·kotlin
帅得不敢出门9 小时前
Linux服务器编译android报no space left on device导致失败的定位解决
android·linux·服务器
雨白10 小时前
协程间的通信管道 —— Kotlin Channel 详解
android·kotlin
TimeFine11 小时前
kotlin协程 容易被忽视的CompletableDeferred
android