11package com .foxdebug .system ;
22
33import android .app .Activity ;
4- import android .graphics .Rect ;
54import android .view .View ;
65
6+ import androidx .core .graphics .Insets ;
7+ import androidx .core .view .ViewCompat ;
8+ import androidx .core .view .WindowInsetsCompat ;
9+ import androidx .core .view .WindowInsetsAnimationCompat ;
10+
711public class SoftInputAssist {
8- private final View rootView ;
9- private final View contentView ;
10- private int baseline = -1 ;
1112
1213 public SoftInputAssist (Activity activity ) {
13- rootView = activity .getWindow ().getDecorView ();
14- contentView = activity .findViewById (android .R .id .content );
14+ View contentView = activity .findViewById (android .R .id .content );
1515
16- rootView .getViewTreeObserver ().addOnGlobalLayoutListener (() -> {
17- Rect r = new Rect ();
18- rootView .getWindowVisibleDisplayFrame (r );
16+ ViewCompat .setWindowInsetsAnimationCallback (
17+ contentView ,
18+ new WindowInsetsAnimationCompat .Callback (
19+ WindowInsetsAnimationCompat .Callback .DISPATCH_MODE_CONTINUE_ON_SUBTREE
20+ ) {
21+ @ Override
22+ public WindowInsetsCompat onProgress (
23+ WindowInsetsCompat insets ,
24+ java .util .List <WindowInsetsAnimationCompat > runningAnimations ) {
1925
20- int heightDiff = rootView .getHeight () - (r .bottom - r .top );
26+ Insets ime = insets .getInsets (WindowInsetsCompat .Type .ime ());
27+ Insets nav = insets .getInsets (WindowInsetsCompat .Type .systemBars ());
2128
22- // Save baseline (system bars only)
23- if (baseline == -1 || heightDiff < baseline ) {
24- baseline = heightDiff ;
25- }
29+ int keyboardHeight = Math .max (0 , ime .bottom - nav .bottom );
2630
27- int keyboardHeight = heightDiff - baseline ;
31+ contentView . setPadding ( 0 , 0 , 0 , keyboardHeight ) ;
2832
29- if (keyboardHeight > 0 ) {
30- contentView .setPadding (0 , 0 , 0 , keyboardHeight );
31- } else {
32- contentView .setPadding (0 , 0 , 0 , 0 );
33+ return insets ;
34+ }
3335 }
34- } );
36+ );
3537 }
3638}
0 commit comments