1 fragment 中的布局使用RecycleView才会使软键盘将View顶上去(外部是FramLayout容器)
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh_layout"
android:background="@color/white"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
<com.zjxnkj.countrysidecommunity.view.CountrySideRecyclerView
android:id="@+id/rv_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
//这里是一个Edittext+TextView(发送) 下面是里边的内容
<include
layout="@layout/include_import"
android:visibility="invisible" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:id="@+id/rl_import"
android:background="@color/aliceblue"
android:gravity="center_vertical">
<TextView
android:id="@+id/send"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@color/red"
android:gravity="center"
android:text="发送"
android:textColor="@color/white"
android:textSize="16sp" />
<EditText
android:id="@+id/dynamicstate_comments_EditText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/send"
android:background="@drawable/edit_bg" />
</RelativeLayout>
2 activity不需要什么设置
<activity
android:name=".activity.MainActivity"
android:launchMode="singleTask" />
3 进入正题 显示软键盘
InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
private void showInput(String hint) {
rlImport.setVisibility(View.VISIBLE);
editText.requestFocus();
editText.setFocusable(true);
editText.setHint(hint);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
// imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
}
4 调整 RecycleView的点击条目底部刚好是Edittext容器的顶部
linearLayoutManager = new LinearLayoutManager(activity);
countrySideAdapter = new CountrySideAdapter(rowsBeans);
rvView.setLayoutManager(linearLayoutManager);
//评论
private void comment(int position) {
showInput("请输入内容");
final View itemCircle = linearLayoutManager.findViewByPosition(position);
new Handler().postDelayed(() -> {
int scrollLocation = getScrollLocation(itemCircle);
rvView.smoothScrollBy(0, scrollLocation);
}, 400);
}
public int getScrollLocation(View itemView) {
int[] importLocation = new int[2];
rlImport.getLocationOnScreen(importLocation);
itemView.getHeight();
int[] itemViewLocation = new int[2];
itemView.getLocationOnScreen(itemViewLocation);
return (itemViewLocation[1] + itemView.getHeight()) - importLocation[1];
}
5 点击外部(软键盘+底部rlImport之外的部分)隐藏软键盘
public void hideInputWindow(Activity context) {
if (editText != null && editText.getWindowToken() != null) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
rlImport.setVisibility(View.INVISIBLE);
}
activity中监听分发事件
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
View view = getCurrentFocus();
if (isShouldHideInput(view, ev)) {
/*InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}*/
Message msg = Message.obtain();
msg.what = Constans.HIDE_INPUT;
EventBus.getDefault().post(msg);
}
break;
}
return super.dispatchTouchEvent(ev);
}
private boolean isShouldHideInput(View view, MotionEvent ev) {
// if (view != null && view.getId() == R.id.rl_import) instanceof EditText) {
if (view != null && view.getId() == R.id.dynamicstate_comments_EditText) {
int[] locationXY = new int[2];
view.getLocationInWindow(locationXY);
int leftX = locationXY[0];
int rightX = locationXY[0] + view.getWidth();
int topY = locationXY[1];
int bottomY = locationXY[1] + view.getHeight();
float downX = ev.getX();
float downY = ev.getY();
if (downY >= topY && downY <= bottomY) {
return false;
}
return true;
} else {
return false;
}
}
//frament中接受事件(是显示的情况下进行隐藏)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(Message message) {
switch (message.what) {
case Constans.HIDE_INPUT:
if (getUserVisibleHint()) {
hideInputWindow(activity);
}
break;
}
}