Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lishouxian first pull request #31

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.view.ViewGroup;

import cn.dreamtobe.kpswitch.IPanelConflictLayout;
import cn.dreamtobe.kpswitch.util.KeyboardUtil;
import cn.dreamtobe.kpswitch.util.StatusBarHeightUtil;
import cn.dreamtobe.kpswitch.util.ViewUtil;

Expand Down Expand Up @@ -97,6 +98,9 @@ public void handleBeforeMeasure(final int width, int height) {
}

// 检测到真正的 由于键盘收起触发了本次的布局变化
if (Math.abs(offset) < KeyboardUtil.getMinKeyboardHeight(mTargetRootView.getContext())) {
return;
}

if (offset > 0) {
//键盘弹起 (offset > 0,高度变小)
Expand Down
45 changes: 31 additions & 14 deletions library/src/main/java/cn/dreamtobe/kpswitch/util/KeyboardUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static int getValidPanelHeight(final Context context) {

private static int MAX_PANEL_HEIGHT = 0;
private static int MIN_PANEL_HEIGHT = 0;
private static int MIN_KEYBOARD_HEIGHT = 0;

public static int getMaxPanelHeight(final Resources res) {
if (MAX_PANEL_HEIGHT == 0) {
Expand All @@ -130,6 +131,13 @@ public static int getMinPanelHeight(final Resources res) {
return MIN_PANEL_HEIGHT;
}

public static int getMinKeyboardHeight(Context context) {
if (MIN_KEYBOARD_HEIGHT == 0) {
MIN_KEYBOARD_HEIGHT = context.getResources().getDimensionPixelSize(R.dimen.min_keyboard_height);
}
return MIN_KEYBOARD_HEIGHT;
}


/**
* Recommend invoked by {@link Activity#onCreate(Bundle)}
Expand All @@ -141,25 +149,37 @@ public static int getMinPanelHeight(final Resources res) {
* @param listener the listener to listen in: keyboard is showing or not.
* @see #saveKeyboardHeight(Context, int)
*/
public static void attach(final Activity activity, IPanelHeightTarget target,
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity, IPanelHeightTarget target,
/** Nullable **/OnKeyboardShowingListener listener) {
final ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
final boolean isFullScreen = ViewUtil.isFullScreen(activity);
final boolean isTranslucentStatus = ViewUtil.isTranslucentStatus(activity);
final boolean isFitSystemWindows = ViewUtil.isFitsSystemWindows(activity);

contentView.getViewTreeObserver().
addOnGlobalLayoutListener(
new KeyboardStatusListener(isFullScreen, isTranslucentStatus,
isFitSystemWindows,
contentView, target, listener));
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new KeyboardStatusListener(
isFullScreen,
isTranslucentStatus,
isFitSystemWindows,
contentView, target, listener);
contentView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
return globalLayoutListener;
}

/**
* @see #attach(Activity, IPanelHeightTarget, OnKeyboardShowingListener)
*/
public static void attach(final Activity activity, IPanelHeightTarget target) {
attach(activity, target, null);
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity, IPanelHeightTarget target) {
return attach(activity, target, null);
}

/**
* Remove the OnGlobalLayoutListener from the activity root view
* @param activity same activity used in {@link #attach} method
* @param l ViewTreeObserver.OnGlobalLayoutListener returned by {@link #attach} method
*/
public static void detach(Activity activity, ViewTreeObserver.OnGlobalLayoutListener l) {
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
contentView.getViewTreeObserver().removeGlobalOnLayoutListener(l);
}

private static class KeyboardStatusListener implements ViewTreeObserver.OnGlobalLayoutListener {
Expand Down Expand Up @@ -236,7 +256,7 @@ private void calculateKeyboardHeight(final int displayHeight) {
keyboardHeight = Math.abs(displayHeight - previousDisplayHeight);
}
// no change.
if (keyboardHeight <= 0) {
if (keyboardHeight <= getMinKeyboardHeight(getContext())) {
return;
}

Expand Down Expand Up @@ -304,11 +324,8 @@ private void calculateKeyboardShowing(final int displayHeight) {
if (maxOverlayLayoutHeight == 0) {
// non-used.
isKeyboardShowing = lastKeyboardShowing;
} else if (displayHeight >= maxOverlayLayoutHeight) {
isKeyboardShowing = false;
} else {
isKeyboardShowing = true;
}
} else
isKeyboardShowing = displayHeight < maxOverlayLayoutHeight - getMinKeyboardHeight(getContext());

maxOverlayLayoutHeight = Math.max(maxOverlayLayoutHeight, actionBarOverlayLayoutHeight);
}
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<resources>
<dimen name="min_panel_height">220dp</dimen>
<dimen name="max_panel_height">380dp</dimen>

<dimen name="min_keyboard_height">80dp</dimen>
</resources>