每当键盘弹出时,如何自动向上滚动软键盘上方的聊天消息(使用recyclerview而不是listview)? [英] How to auto scroll up the chat messages (using recyclerview instead of listview) above the softkeypad whenever keypad pop ups?

本文介绍了每当键盘弹出时,如何自动向上滚动软键盘上方的聊天消息(使用recyclerview而不是listview)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像聊天应用程序一样,每当我们要发送消息时,弹出软键盘都会弹出,如果没有隐藏在软键盘后面,该弹出窗口还会自动将最后看到的消息滚动到软键盘的顶部。但就我而言,键盘隐藏了对话。如何解决此问题,我已经习惯了在回收站视图中显示消息。我使用了 android.support.v7.widget.RecyclerView

Like in chat applications whenever we want to send messages soft keypad pop ups which also auto scroll the last seen messages to top of the soft keypad provided that nothing is hidden behind the soft keypad. But in my case the keypad hides the conversations. How to fix this issue and I have used to recycler view for displaying the messages. I used android.support.v7.widget.RecyclerView

推荐答案

在RecyclerView中,您可以通过以下方式实现此目标:

With RecyclerView you can achieve this as follows:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);

此代码块将告诉RecyclerView相对于列表底部滚动,但同时显示消息的顺序相反,因此,如果您从数据库中获取消息,请从代码中读取它们,如下所示:

This block of code will tell the RecyclerView to scroll relative to the bottom of the list but also it shows the messages in reverse order so in your code if you are getting the messages from database, read them from the last message like this:

Cursor c = ...;
c.moveToLast();
do{
//your code which gets messages from cursor...
}while(c.moveToPrevoius());

,当您要将新消息添加到列表中时,只需像这样添加它们:

and when you want to add a new message to the list just add them like this:

//ArrayList messages
messages.add(0, message);

这篇关于每当键盘弹出时,如何自动向上滚动软键盘上方的聊天消息(使用recyclerview而不是listview)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆