lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
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
99
100
101
102
103
104
105
106
107
package com.fuban.driver.utils;
 
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
 
import java.util.LinkedList;
import java.util.List;
 
/***
 * 监听键盘打开关闭 并回调高度
 */
public class SoftKeyBroadManager implements ViewTreeObserver.OnGlobalLayoutListener{
 
    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed(int keyboardHeightInPx);
    }
 
    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
    private final View activityRootView;
    private int lastSoftKeyboardHeightInPx;
    private boolean isSoftKeyboardOpened;
 
    public SoftKeyBroadManager(View activityRootView) {
        this(activityRootView,false);
    }
 
    public SoftKeyBroadManager(View activityRootView, boolean isSoftKeyboardOpened) {
        listeners.add(OneManager);
        this.activityRootView = activityRootView;
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }
 
    public static int height_jp;
 
    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        activityRootView.getWindowVisibleDisplayFrame(r);
        int rootHeight = activityRootView.getRootView().getHeight();
        final int heightDiff = rootHeight - (r.bottom - r.top);
        if (!isSoftKeyboardOpened && heightDiff > 500) {
// 如果高度超过500 键盘可能被打开
            isSoftKeyboardOpened = true;
            height_jp = heightDiff;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < 500) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed(height_jp);
        }
    }
 
    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }
 
    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }
 
 
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }
 
    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }
 
    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }
 
    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
 
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }
 
    private void notifyOnSoftKeyboardClosed(int h) {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed(h);
            }
        }
    }
 
    public SoftKeyboardStateListener OneManager = new SoftKeyboardStateListener() {
        @Override
        public void onSoftKeyboardOpened(int keyboardHeightInPx) {
            activityRootView.setScrollY(keyboardHeightInPx);
        }
 
        @Override
        public void onSoftKeyboardClosed(int keyboardHeightInPx) {
            activityRootView.setScrollY(0);
        }
 
    };
 
}