|
2 | 2 |
|
3 | 3 | import android.app.Activity; |
4 | 4 | import android.view.View; |
| 5 | +import java.util.List; |
5 | 6 |
|
6 | 7 | import androidx.core.graphics.Insets; |
7 | 8 | import androidx.core.view.ViewCompat; |
|
10 | 11 |
|
11 | 12 | public class SoftInputAssist { |
12 | 13 |
|
| 14 | + private boolean animationRunning = false; |
| 15 | + |
13 | 16 | public SoftInputAssist(Activity activity) { |
14 | 17 | View contentView = activity.findViewById(android.R.id.content); |
15 | 18 |
|
| 19 | + ViewCompat.setOnApplyWindowInsetsListener(contentView, (v, insets) -> { |
| 20 | + |
| 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); |
| 26 | + |
| 27 | + v.setPadding(0, 0, 0, keyboardHeight); |
| 28 | + } |
| 29 | + |
| 30 | + return insets; |
| 31 | + }); |
| 32 | + |
16 | 33 | ViewCompat.setWindowInsetsAnimationCallback( |
17 | 34 | contentView, |
18 | 35 | new WindowInsetsAnimationCompat.Callback( |
19 | 36 | WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE |
20 | 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 | + |
21 | 49 | @Override |
22 | 50 | public WindowInsetsCompat onProgress( |
23 | 51 | WindowInsetsCompat insets, |
24 | | - java.util.List<WindowInsetsAnimationCompat> runningAnimations) { |
| 52 | + List<WindowInsetsAnimationCompat> runningAnimations) { |
25 | 53 |
|
26 | 54 | Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime()); |
27 | | - Insets nav = insets.getInsets(WindowInsetsCompat.Type.systemBars()); |
| 55 | + Insets nav = insets.getInsets(WindowInsetsCompat.Type.navigationBars()); |
28 | 56 |
|
29 | 57 | int keyboardHeight = Math.max(0, ime.bottom - nav.bottom); |
30 | 58 |
|
|
0 commit comments