KeyBoardObservable.java
2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.wd.speech.view;
import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import com.wd.speech.utils.KeyBoardObserver;
import com.wd.speech.utils.SharedPreferencesUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author LiuKun
* @date 2023/5/2 17:26
* @Description:监听器
*/
public class KeyBoardObservable {
private int lastHeight;
private List<KeyBoardObserver> observers;
private boolean keyBoardVisibility;
/**
* 注册监听
*
* @param listener
*/
public void register(@NonNull KeyBoardObserver listener) {
if (observers == null) {
observers = new ArrayList<>();
}
observers.add(listener);
}
/**
* 注销
*
* @param listener
*/
public void unRegister(@NonNull KeyBoardObserver listener) {
if (observers != null) {
observers.remove(listener);
}
}
/**
* 抢先测量
*
* @param heightMeasureSpec
*/
public void beforeMeasure(Context context, int heightMeasureSpec) {
int height = View.MeasureSpec.getSize(heightMeasureSpec);
if (lastHeight == 0) {
lastHeight = height;
return;
}
if (lastHeight == height) {
//没有发生挤压
return;
}
final int offset = lastHeight - height;
/*if (Math.abs(offset) < DensityUtil.dp2px(80)) {
//80 dp 挤压阈值
return;
}*/
if (offset > 0) {
keyBoardVisibility = true;
} else {
keyBoardVisibility = false;
}
int keyBoardHeight = Math.abs(offset);
SharedPreferencesUtils.saveKeyBoardHeight(context, keyBoardHeight);
update(keyBoardVisibility, keyBoardHeight);
lastHeight = height;
}
public boolean isKeyBoardVisibility() {
return keyBoardVisibility;
}
/**
* 通知更新
*
* @param keyBoardVisibility
*/
private void update(final boolean keyBoardVisibility, int keyBoardHeight) {
if (observers != null) {
for (KeyBoardObserver observable : observers) {
observable.update(keyBoardVisibility, keyBoardHeight);
}
}
}
}