4
4
import android .support .design .widget .BottomNavigationView ;
5
5
import android .support .v4 .view .ViewPager ;
6
6
import android .view .MenuItem ;
7
+ import android .widget .LinearLayout ;
7
8
9
+ import com .hjq .baselibrary .utils .OnClickUtils ;
8
10
import com .hjq .demo .R ;
9
11
import com .hjq .demo .common .CommonActivity ;
10
12
import com .hjq .demo .ui .adapter .HomeViewPagerAdapter ;
11
- import com .hjq .baselibrary .utils .OnClickUtils ;
12
13
import com .hjq .toast .ToastUtils ;
13
14
14
15
import butterknife .BindView ;
20
21
* desc : 主页界面
21
22
*/
22
23
public class HomeActivity extends CommonActivity implements
23
- ViewPager .OnPageChangeListener , BottomNavigationView .OnNavigationItemSelectedListener {
24
+ ViewPager .OnPageChangeListener , Runnable ,
25
+ BottomNavigationView .OnNavigationItemSelectedListener {
24
26
25
27
@ BindView (R .id .vp_home_pager )
26
28
ViewPager mViewPager ;
27
29
@ BindView (R .id .bv_home_navigation )
28
30
BottomNavigationView mBottomNavigationView ;
29
31
32
+ private HomeViewPagerAdapter mAdapter ;
33
+
30
34
@ Override
31
35
protected int getLayoutId () {
32
36
return R .layout .activity_home ;
@@ -39,18 +43,40 @@ protected int getTitleBarId() {
39
43
40
44
@ Override
41
45
protected void initView () {
42
- // 限制页面数量
43
- mViewPager .setOffscreenPageLimit (4 );
44
46
mViewPager .addOnPageChangeListener (this );
45
47
46
48
// 不使用图标默认变色
47
49
mBottomNavigationView .setItemIconTintList (null );
48
50
mBottomNavigationView .setOnNavigationItemSelectedListener (this );
51
+
52
+ // 修复在 ViewPager 中点击 EditText 弹出软键盘导致 BottomNavigationView 还显示在 ViewPager 下面的问题
53
+ postDelayed (this , 1000 );
49
54
}
50
55
51
56
@ Override
52
57
protected void initData () {
53
- mViewPager .setAdapter (new HomeViewPagerAdapter (this ));
58
+ mAdapter = new HomeViewPagerAdapter (this );
59
+ mViewPager .setAdapter (mAdapter );
60
+
61
+ // 限制页面数量
62
+ mViewPager .setOffscreenPageLimit (mAdapter .getCount ());
63
+ }
64
+
65
+ /**
66
+ * {@link Runnable}
67
+ */
68
+ @ Override
69
+ public void run () {
70
+ /*
71
+ 父布局为LinearLayout,因为 ViewPager 使用了权重的问题
72
+ 软键盘在弹出的时候会把布局进行收缩,ViewPager 的高度缩小了
73
+ 所以 BottomNavigationView 会显示在ViewPager 下面
74
+ 解决方法是在 ViewPager 初始化高度后手动进行设置 ViewPager 高度并将权重设置为0
75
+ */
76
+ LinearLayout .LayoutParams layoutParams = (LinearLayout .LayoutParams ) mViewPager .getLayoutParams ();
77
+ layoutParams .height = mViewPager .getHeight ();
78
+ layoutParams .weight = 0 ;
79
+ mViewPager .setLayoutParams (layoutParams );
54
80
}
55
81
56
82
/**
@@ -134,4 +160,4 @@ public boolean isSupportSwipeBack() {
134
160
// 不使用侧滑功能
135
161
return !super .isSupportSwipeBack ();
136
162
}
137
- }
163
+ }
0 commit comments