【问题标题】:How to achieve chat screen type soft keyboard behaviour?如何实现聊天屏幕类型的软键盘行为?
【发布时间】:2018-09-18 11:40:04
【问题描述】:

我正在尝试在示例聊天应用程序中实现特定的键盘行为,其中当用户打开键盘时,RecyclerView 会随键盘一起上升,但前提是最后一个聊天可见。

示例:在WhatsApp 打开键盘时,当最后一条消息可见时,列表将随键盘上升,以保持对用户可见。现在向上滚动一点,然后打开键盘,现在列表将保持原样,不会随键盘上升。

【问题讨论】:

    标签: android android-recyclerview android-softkeyboard


    【解决方案1】:

    为了实现随着键盘向上滚动列表的行为,我从这里得出了解决方案:https://stackoverflow.com/a/34103263/1739882

    为了添加决定是否滚动列表的自定义行为,我用这个获取了最后一个可见项目:

    linearLayoutManager.findLastVisibleItemPosition()
    

    并使用标志来处理场景。完整代码如下:

    //Setup editText behavior for opening soft keyboard
        activityChatHeadBinding.edtMsg.setOnTouchListener((v, event) -> {
            InputMethodManager keyboard = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (keyboard != null) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) activityChatHeadBinding.recyclerView.getLayoutManager();
                isScrollToLastRequired = linearLayoutManager.findLastVisibleItemPosition() == chatNewAdapter.getItemCount() - 1;
                keyboard.showSoftInput(findViewById(R.id.layout_send_msg), InputMethodManager.SHOW_FORCED);
            }
            return false;
        });
    
    //Executes recycler view scroll if required.
    activityChatHeadBinding.recyclerView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
                if (bottom < oldBottom && isScrollToLastRequired) {
                    activityChatHeadBinding.recyclerView.postDelayed(() -> activityChatHeadBinding.recyclerView.scrollToPosition(
                            activityChatHeadBinding.recyclerView.getAdapter().getItemCount() - 1), 100);
                }
            });
    

    【讨论】:

      【解决方案2】:

      您可以直接在活动标签内的清单文件中添加 windowSoftInputMode ,如下所示:-

      <activity android:name=".ui.chat.ChatActivity"
              android:windowSoftInputMode="adjustResize|stateAlwaysHidden"/>
      

      有什么问题可以问。

      【讨论】:

        【解决方案3】:

        只需添加

        android:windowSoftInputMode = "adjustResize"
        

        在清单中的活动标签内。

        【讨论】:

          猜你喜欢
          • 2015-10-26
          • 1970-01-01
          • 2016-06-01
          • 2013-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-01
          • 2023-02-14
          相关资源
          最近更新 更多