RecycleView (通过LinearLayoutManager)显示软键盘,点击外部隐藏软键盘

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;
        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值