需求是这样的,上面一个封面图,下面一个tablayout关联viewpager。问题就在于viewpager内有个融云的聊天模块Fragment,完事儿一点editText,软键盘就遮挡住editText了(透)。
由于coordinatorLayout没有自动收放的功能(透),所以想了两个方案:
1.软键盘直接把activity顶出去(这个被否了)
2.coordinatorLayout内封面收起,viewpager长度重置。
1.manifast内配置Activity的windowSoftInputMode="adjustResize",有滚动控件的布局,采用的adjustResize方式。
< android:name=".activity.TuWenLiveActivity"
android:windowSoftInputMode="adjustResize" />
2.AppbarLayout配置展开和收起状态下viewpager的高度。
至于为什么要重新配置不用matchparent,因为这有个tablayout!!!左边划过来右边反应不过来!不重新配置一下的话聊天界面的输入框飞出屏幕外了。
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
barstate =state;
if (state ==State.EXPANDED) {
//展开状态
ViewGroup.LayoutParams layoutParams =viewPager.getLayoutParams();
if (slidingtabs.getSelectedTabPosition() ==1) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int heightPixels =displayMetrics.heightPixels;
int top = (int) (displayMetrics.widthPixels *0.56);
int i =heightPixels -top -DimensionUtils.getStatusBarHeight(TuWenLiveActivity.this) -DimensionUtils.dip2px(TuWenLiveActivity.this, 42);
layoutParams.height =i;
}else {
layoutParams.height =ViewGroup.LayoutParams.MATCH_PARENT;
}
viewPager.setLayoutParams(layoutParams);
}else if (state ==State.COLLAPSED) {
//折叠状态
ViewGroup.LayoutParams layoutParams =viewPager.getLayoutParams();
if (slidingtabs.getSelectedTabPosition() ==1) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int heightPixels =displayMetrics.heightPixels;
int i =heightPixels -DimensionUtils.getStatusBarHeight(TuWenLiveActivity.this) -DimensionUtils.dip2px(TuWenLiveActivity.this, 42);
layoutParams.height =i;
}else {
layoutParams.height =ViewGroup.LayoutParams.MATCH_PARENT;
}
viewPager.setLayoutParams(layoutParams);
}else {
//中间状态
}
}
});
3.监听键盘收放状态,再配置一次viewpager的高度。
有个特点是,键盘展开的时候,coordinatoLayout上面的收缩部分会自动收缩!所以键盘展开的时候聊天页面一定是全屏的,这时候收起键盘只用让viewpager变成全屏就行了。至于增加的那个是否展开的判断,是因为防止进来的时候展开状态响应,聊天界面被顶出去。
@Override
protected void onResume() {
super.onResume();
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//获取View可见区域的bottom
Rect rect =new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
if (bottom !=0 &&oldBottom !=0 &&bottom -rect.bottom <=0) {
//收起
if (barstate ==AppBarStateChangeListener.State.EXPANDED) {
}else {
ViewGroup.LayoutParams layoutParams =viewPager.getLayoutParams();
if (slidingtabs.getSelectedTabPosition() ==1) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int heightPixels =displayMetrics.heightPixels;
int i =heightPixels -DimensionUtils.getStatusBarHeight(TuWenLiveActivity.this) -DimensionUtils.dip2px(TuWenLiveActivity.this, 42);
layoutParams.height =i;
}else {
layoutParams.height =ViewGroup.LayoutParams.MATCH_PARENT;
}
viewPager.setLayoutParams(layoutParams);
}
}else {
//弹出
ViewGroup.LayoutParams layoutParams =viewPager.getLayoutParams();
if (slidingtabs.getSelectedTabPosition() ==1) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int heightPixels =displayMetrics.heightPixels;
int b =bottom -rect.bottom;
int i =heightPixels -DimensionUtils.getStatusBarHeight(TuWenLiveActivity.this) -DimensionUtils.dip2px(TuWenLiveActivity.this, 42) -b;
layoutParams.height =i;
}else {
layoutParams.height =ViewGroup.LayoutParams.MATCH_PARENT;
}
viewPager.setLayoutParams(layoutParams);
}
}
});
}