Skip to content

Commit a01286a

Browse files
committed
fix keyboard height changed after popped
1 parent fd06923 commit a01286a

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

keyboard/src/main/java/cn/hadcn/keyboard/utils/Utils.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,11 @@ public static void setDefKeyboardHeight(Context context, int height) {
6262
Utils.sDefKeyboardHeight = height;
6363
}
6464

65-
private static int mDisplayWidthPixels = 0;
66-
private static int mDisplayHeightPixels = 0;
67-
68-
private static void getDisplayMetrics(Context context) {
69-
WindowManager wm = (WindowManager) context.
70-
getSystemService(Context.WINDOW_SERVICE);
71-
DisplayMetrics dm = new DisplayMetrics();
72-
wm.getDefaultDisplay().getMetrics(dm);
73-
mDisplayWidthPixels = dm.widthPixels;
74-
mDisplayHeightPixels = dm.heightPixels;
75-
}
76-
7765
public static int getDisplayHeightPixels(Context context) {
78-
if (context == null) {
79-
return -1;
80-
}
81-
if (mDisplayHeightPixels == 0) {
82-
getDisplayMetrics(context);
83-
}
84-
return mDisplayHeightPixels;
85-
}
86-
87-
public static int getDisplayWidthPixels(Context context) {
88-
if (context == null) {
89-
return -1;
90-
}
91-
if (mDisplayWidthPixels == 0) {
92-
getDisplayMetrics(context);
93-
}
94-
return mDisplayWidthPixels;
66+
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
67+
DisplayMetrics metrics = new DisplayMetrics();
68+
manager.getDefaultDisplay().getMetrics(metrics);
69+
return metrics.heightPixels;
9570
}
9671

9772
public static int dip2px(Context context, float dipValue) {

keyboard/src/main/java/cn/hadcn/keyboard/view/SoftHandleLayout.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ protected void OnSoftKeyboardPop(int height) {
122122
//if soft keyboard popped, auto view must be visible, soft keyboard covers it
123123
if (mKeyboardState != KEYBOARD_STATE_BOTH) {
124124
showAutoView();
125+
} else {
126+
// else, just update auto view height
127+
postDelayed(new Runnable() {
128+
@Override
129+
public void run() {
130+
setAutoViewHeight(mAutoViewHeight);
131+
}
132+
}, 150);
125133
}
126134
mKeyboardState = KEYBOARD_STATE_BOTH;
127135
}

keyboard/src/main/java/cn/hadcn/keyboard/view/SoftListenLayout.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package cn.hadcn.keyboard.view;
22

3+
import static cn.hadcn.keyboard.utils.Utils.getDisplayHeightPixels;
34
import android.app.Activity;
45
import android.content.Context;
56
import android.graphics.Rect;
67
import android.util.AttributeSet;
7-
import android.util.DisplayMetrics;
8+
import android.util.Log;
89
import android.view.ViewTreeObserver;
9-
import android.view.WindowManager;
1010
import android.widget.RelativeLayout;
1111

1212
import cn.hadcn.keyboard.utils.Utils;
@@ -23,17 +23,15 @@ public abstract class SoftListenLayout extends RelativeLayout {
2323
private int mKeyboardHeight = 0;
2424
protected Context mContext;
2525

26-
public SoftListenLayout(Context context, AttributeSet attrs) {
26+
public SoftListenLayout(final Context context, AttributeSet attrs) {
2727
super(context, attrs);
2828
mContext = context;
29-
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
30-
DisplayMetrics metrics = new DisplayMetrics();
31-
manager.getDefaultDisplay().getMetrics(metrics);
29+
int displayHeight = getDisplayHeightPixels(context);
3230
//the height of layout is at least 2/3 of screen height
33-
mMinLayoutHeight = metrics.heightPixels * 2 / 3;
31+
mMinLayoutHeight = displayHeight * 2 / 3;
3432

3533
// min keyboard height, for ignoring navigation bar hide or show effects
36-
mMinKeyboardHeight = metrics.heightPixels / 3;
34+
mMinKeyboardHeight = displayHeight / 3;
3735

3836
mKeyboardHeight = Utils.getDefKeyboardHeight(mContext);
3937

@@ -44,9 +42,11 @@ public void onGlobalLayout() {
4442
Rect r = new Rect();
4543
((Activity) getContext()).getWindow().getDecorView()
4644
.getWindowVisibleDisplayFrame(r);
47-
if (mGlobalBottom != 0 && mGlobalBottom - r.bottom > mMinKeyboardHeight) {
45+
int popHeight = Utils.getDisplayHeightPixels(context) - r.bottom;
46+
if ((mGlobalBottom != 0 && mGlobalBottom - r.bottom > mMinKeyboardHeight)
47+
|| (popHeight != 0 && mKeyboardHeight != popHeight)) {
4848
// keyboard pop
49-
mKeyboardHeight = mGlobalBottom - r.bottom;
49+
mKeyboardHeight = popHeight;
5050
OnSoftKeyboardPop(mKeyboardHeight);
5151
} else if (mGlobalBottom != 0 && r.bottom - mGlobalBottom > mMinKeyboardHeight) {
5252
OnSoftKeyboardClose();

0 commit comments

Comments
 (0)