Skip to content

Commit 5bd17ad

Browse files
committed
hide tab layout when show keyboard
1 parent b2dc688 commit 5bd17ad

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

lib-n-ide/src/main/java/com/duy/ide/core/IdeActivity.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.SharedPreferences;
2424
import android.content.pm.ActivityInfo;
2525
import android.content.pm.PackageManager;
26+
import android.graphics.Rect;
2627
import android.graphics.drawable.Drawable;
2728
import android.net.Uri;
2829
import android.os.Build;
@@ -46,6 +47,7 @@
4647
import android.view.MenuItem;
4748
import android.view.SubMenu;
4849
import android.view.View;
50+
import android.view.ViewTreeObserver;
4951
import android.widget.TextView;
5052

5153
import com.duy.common.StoreUtil;
@@ -81,6 +83,7 @@
8183
import com.jecelyin.editor.v2.widget.menu.MenuFactory;
8284
import com.jecelyin.editor.v2.widget.menu.MenuGroup;
8385
import com.jecelyin.editor.v2.widget.menu.MenuItemInfo;
86+
import com.ogaclejapan.smarttablayout.SmartTabLayout;
8487
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
8588

8689
import org.gjt.sp.jedit.Catalog;
@@ -104,16 +107,20 @@ public abstract class IdeActivity extends ThemeSupportActivity implements MenuIt
104107

105108
//Handle create on MainThread, use for update UI
106109
private final Handler mHandler = new Handler();
107-
108110
public SlidingUpPanelLayout mSlidingUpPanelLayout;
109-
public Toolbar mToolbar;
110111
public ViewPager mEditorPager;
111-
public DrawerLayout mDrawerLayout;
112+
112113
protected TabManager mTabManager;
113114
protected DiagnosticPresenter mDiagnosticPresenter;
115+
114116
private SymbolBarLayout mSymbolBarLayout;
117+
private Toolbar mToolbar;
118+
private DrawerLayout mDrawerLayout;
119+
private SmartTabLayout mTabLayout;
120+
115121
private Preferences mPreferences;
116122
private long mExitTime;
123+
private KeyBoardEventListener mKeyBoardListener;
117124

118125
@Override
119126
protected void onCreate(Bundle savedInstanceState) {
@@ -125,6 +132,7 @@ protected void onCreate(Bundle savedInstanceState) {
125132
MenuManager.init(this);
126133
mPreferences = Preferences.getInstance(this);
127134

135+
mTabLayout = findViewById(R.id.tab_layout);
128136
mEditorPager = findViewById(R.id.editor_view_pager);
129137
mDrawerLayout = findViewById(R.id.drawer_layout);
130138
mDrawerLayout.setKeepScreenOn(mPreferences.isKeepScreenOn());
@@ -141,10 +149,6 @@ public void onDrawerOpened(View drawerView) {
141149
mDrawerLayout.addDrawerListener(actionBarDrawerToggle);
142150
actionBarDrawerToggle.syncState();
143151

144-
//keyboard hide/show listener
145-
setRootLayout(mDrawerLayout);
146-
attachKeyboardListeners();
147-
148152
mSymbolBarLayout = findViewById(R.id.symbolBarLayout);
149153
mSymbolBarLayout.setOnSymbolCharClickListener(new SymbolBarLayout.OnSymbolCharClickListener() {
150154
@Override
@@ -165,6 +169,10 @@ public void onClick(View v, String text) {
165169
processIntent();
166170

167171
mPreferences.registerOnSharedPreferenceChangeListener(this);
172+
173+
//attach listener hide/show keyboard
174+
mKeyBoardListener = new KeyBoardEventListener(this);
175+
mDrawerLayout.getViewTreeObserver().addOnGlobalLayoutListener(mKeyBoardListener);
168176
}
169177

170178
private void intiDiagnosticView() {
@@ -731,4 +739,44 @@ public String getCurrentLang() {
731739
public void onEditorViewCreated(IEditorDelegate editorDelegate) {
732740

733741
}
742+
743+
protected void onShowKeyboard() {
744+
mTabLayout.setVisibility(View.GONE);
745+
}
746+
747+
protected void onHideKeyboard() {
748+
mTabLayout.setVisibility(View.VISIBLE);
749+
}
750+
751+
@Override
752+
protected void onDestroy() {
753+
mDrawerLayout.getViewTreeObserver()
754+
.removeGlobalOnLayoutListener(mKeyBoardListener);
755+
super.onDestroy();
756+
}
757+
758+
private class KeyBoardEventListener implements ViewTreeObserver.OnGlobalLayoutListener {
759+
IdeActivity activity;
760+
761+
KeyBoardEventListener(IdeActivity activityIde) {
762+
this.activity = activityIde;
763+
}
764+
765+
public void onGlobalLayout() {
766+
int i = 0;
767+
int navHeight = this.activity.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
768+
navHeight = navHeight > 0 ? this.activity.getResources().getDimensionPixelSize(navHeight) : 0;
769+
int statusBarHeight = this.activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
770+
if (statusBarHeight > 0) {
771+
i = this.activity.getResources().getDimensionPixelSize(statusBarHeight);
772+
}
773+
Rect rect = new Rect();
774+
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
775+
if (activity.mDrawerLayout.getRootView().getHeight() - ((navHeight + i) + rect.height()) <= 0) {
776+
activity.onHideKeyboard();
777+
} else {
778+
activity.onShowKeyboard();
779+
}
780+
}
781+
}
734782
}

lib-n-ide/src/main/java/com/jecelyin/editor/v2/ThemeSupportActivity.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
import android.support.v7.app.AppCompatDelegate;
2626
import android.view.MenuItem;
2727
import android.view.View;
28-
import android.view.ViewGroup;
29-
import android.view.ViewTreeObserver;
30-
import android.view.Window;
3128
import android.view.WindowManager;
3229

3330
import com.duy.ide.editor.editor.R;
@@ -38,38 +35,7 @@
3835
*/
3936
public abstract class ThemeSupportActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
4037
private static final String TAG = "BaseActivity";
41-
private boolean keyboardListenersAttached = false;
42-
@Nullable
43-
private ViewGroup rootLayout;
44-
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
45-
@Override
46-
public void onGlobalLayout() {
47-
int heightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
48-
int contentViewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
49-
if (heightDiff <= contentViewTop) {
50-
onHideKeyboard();
51-
} else {
52-
int keyboardHeight = heightDiff - contentViewTop;
53-
onShowKeyboard(keyboardHeight);
54-
}
55-
}
56-
};
57-
58-
protected void onShowKeyboard(int keyboardHeight) {
59-
}
6038

61-
protected void onHideKeyboard() {
62-
}
63-
64-
protected void attachKeyboardListeners() {
65-
if (keyboardListenersAttached) {
66-
return;
67-
}
68-
if (rootLayout != null) {
69-
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
70-
keyboardListenersAttached = true;
71-
}
72-
}
7339

7440
@Override
7541
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -130,18 +96,4 @@ protected void onStop() {
13096
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
13197
}
13298

133-
@Override
134-
protected void onDestroy() {
135-
super.onDestroy();
136-
137-
if (keyboardListenersAttached) {
138-
if (rootLayout != null) {
139-
rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener);
140-
}
141-
}
142-
}
143-
144-
public void setRootLayout(@Nullable ViewGroup view) {
145-
rootLayout = view;
146-
}
14799
}

0 commit comments

Comments
 (0)