You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WindowInsetsAnimationCompat.Callback的onProgress、onEnd方法仅在应用前后台切换后触发的问题求助

WindowInsetsAnimationCompat.Callback的onProgress、onEnd方法仅在应用前后台切换后触发的问题求助

各位好,我最近在实现软键盘弹出时的Insets动画监听,碰到了一个百思不得其解的问题,想请大家帮忙看看。

我写了一个继承自WindowInsetsAnimationCompat.Callback的InsetsWithKeyboardAnimationCallbackHelper类,同时实现了OnApplyWindowInsetsListener,用来监听软键盘弹出时的Insets动画进度和结束事件。初始化时我通过ViewCompat.setWindowInsetsAnimationCallback给父View绑定了这个回调,代码如下:

final WindowInsetsAnimationCompat.Callback insetsWithKeyboardAnimationCallback = new InsetsWithKeyboardAnimationCallbackHelper(getWindow(), imageLayout);
ViewCompat.setWindowInsetsAnimationCallback(parentView, insetsWithKeyboardAnimationCallback);

我的Helper类代码是这样的:

public class InsetsWithKeyboardAnimationCallbackHelper extends WindowInsetsAnimationCompat.Callback implements OnApplyWindowInsetsListener {
    private final Window window;
    private final View view;
    private final String TAG = this.getClass().getSimpleName();

    public InsetsWithKeyboardAnimationCallbackHelper(Window window, View view) {
        super(WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE);
        this.window = window;
        this.view = view;
    }

    @Override
    public void onPrepare(@NonNull WindowInsetsAnimationCompat animation) {
        Log.d(TAG, "onPrepare");
        super.onPrepare(animation);
    }

    @NonNull
    @Override
    public WindowInsetsCompat onProgress(@NonNull WindowInsetsCompat insets, @NonNull List<WindowInsetsAnimationCompat> runningAnimations) {
        Log.d(TAG, "onProgress");
        return insets;
    }

    @Override
    public void onEnd(@NonNull WindowInsetsAnimationCompat animation) {
        Log.d(TAG, "onEnd");
        super.onEnd(animation);
    }
}

奇怪的问题来了:第一次打开软键盘的时候,onProgress和onEnd方法完全没有被调用,日志根本打不出来;可是如果我把应用切到后台,再切回到前台,这时候再打开键盘,onProgress和onEnd就正常触发了,日志也能正常打印。

有没有大佬遇到过类似的问题?或者能帮我分析下可能的原因是什么吗?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 09:58:03