22
33import android .app .Activity ;
44import android .view .View ;
5- import java .util .List ;
65
76import androidx .core .graphics .Insets ;
87import androidx .core .view .ViewCompat ;
98import androidx .core .view .WindowInsetsCompat ;
10- import androidx .core .view .WindowInsetsAnimationCompat ;
119
1210public class SoftInputAssist {
1311
14- private boolean animationRunning = false ;
15-
1612 public SoftInputAssist (Activity activity ) {
1713 View contentView = activity .findViewById (android .R .id .content );
1814
1915 ViewCompat .setOnApplyWindowInsetsListener (contentView , (v , insets ) -> {
16+ Insets ime = insets .getInsets (WindowInsetsCompat .Type .ime ());
17+ Insets nav = insets .getInsets (WindowInsetsCompat .Type .navigationBars ());
2018
21- if (!animationRunning ) {
22- Insets ime = insets .getInsets (WindowInsetsCompat .Type .ime ());
23- Insets nav = insets .getInsets (WindowInsetsCompat .Type .navigationBars ());
24-
25- int keyboardHeight = Math .max (0 , ime .bottom - nav .bottom );
19+ int keyboardHeight = Math .max (0 , ime .bottom - nav .bottom );
2620
27- v .setPadding (0 , 0 , 0 , keyboardHeight );
28- }
21+ v .setPadding (0 , 0 , 0 , keyboardHeight );
2922
3023 return insets ;
3124 });
3225
33- ViewCompat .setWindowInsetsAnimationCallback (
34- contentView ,
35- new WindowInsetsAnimationCompat .Callback (
36- WindowInsetsAnimationCompat .Callback .DISPATCH_MODE_CONTINUE_ON_SUBTREE
37- ) {
38-
39- @ Override
40- public void onPrepare (WindowInsetsAnimationCompat animation ) {
41- animationRunning = true ;
42- }
43-
44- @ Override
45- public void onEnd (WindowInsetsAnimationCompat animation ) {
46- animationRunning = false ;
47- }
48-
49- @ Override
50- public WindowInsetsCompat onProgress (
51- WindowInsetsCompat insets ,
52- List <WindowInsetsAnimationCompat > runningAnimations ) {
53-
54- Insets ime = insets .getInsets (WindowInsetsCompat .Type .ime ());
55- Insets nav = insets .getInsets (WindowInsetsCompat .Type .navigationBars ());
56-
57- int keyboardHeight = Math .max (0 , ime .bottom - nav .bottom );
58-
59- contentView .setPadding (contentView .getPaddingLeft (), contentView .getPaddingTop (), contentView .getPaddingRight (), keyboardHeight );
60-
61- return insets ;
62- }
63- }
64- );
26+ ViewCompat .requestApplyInsets (contentView );
6527 }
66- }
28+ }
0 commit comments