gpt4 book ai didi

android - 如何实现聊天屏幕类型的软键盘行为?

转载 作者:行者123 更新时间:2023-11-29 23:34:12 28 4
gpt4 key购买 nike

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

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

最佳答案

为了实现随着键盘向上滚动列表的行为,我从这里得出解决方案: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);
}
});

关于android - 如何实现聊天屏幕类型的软键盘行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52385793/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com