Android 当键盘弹出时,如何在软键盘上方自动向上滚动聊天信息(使用recyclerview而不是listview)?

Android 当键盘弹出时,如何在软键盘上方自动向上滚动聊天信息(使用recyclerview而不是listview)?,android,android-fragments,android-recyclerview,android-design-library,android-developer-api,Android,Android Fragments,Android Recyclerview,Android Design Library,Android Developer Api,就像在聊天应用程序中一样,每当我们想要发送消息时,软键盘弹出窗口也会自动将最后看到的消息滚动到软键盘顶部,前提是软键盘后面没有隐藏任何内容。但在我的例子中,键盘隐藏了对话。如何解决这个问题,我使用了recycler视图来显示消息。我使用了android.support.v7.widget.RecyclerView,你可以在编辑文本上检测焦点,如果你的编辑文本有焦点, 将recyclerView列表滚动到最后一个元素 private OnFocuseChangeListener focuseLis

就像在聊天应用程序中一样,每当我们想要发送消息时,软键盘弹出窗口也会自动将最后看到的消息滚动到软键盘顶部,前提是软键盘后面没有隐藏任何内容。但在我的例子中,键盘隐藏了对话。如何解决这个问题,我使用了recycler视图来显示消息。我使用了android.support.v7.widget.RecyclerView,你可以在编辑文本上检测焦点,如果你的编辑文本有焦点, 将recyclerView列表滚动到最后一个元素

private OnFocuseChangeListener focuseListener = new OnFocuseChangeListener(){
  public void onFocusChange(View v, boolean hasFocus){
            if(hasFocus){

            // Scroll down to the last element of the list
            recyclerView.scrollToPosition(sizeOfYourList);

          } else {
              focusedView  = null;
        }
    }
}

类似地,当您单击发送按钮或要显示列表最后一个元素的位置时,请使用scrollToPosition()。

使用RecyclerView,您可以通过以下方式实现此目的:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);
这段代码将告诉RecyclerView相对于列表底部滚动,但它也会以相反的顺序显示消息,因此在代码中,如果您是从数据库中获取消息,请按照如下方式从最后一条消息中读取它们:

Cursor c = ...;
c.moveToLast();
do{
//your code which gets messages from cursor...
}while(c.moveToPrevoius());
//ArrayList messages
messages.add(0, message);
当您想向列表中添加新消息时,只需按如下方式添加它们:

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

我如何处理这一点非常简单,实现一个良好的行为,类似于Whatsapp或其他流行聊天

首先,像这样设置
LinearLayoutManager

 new LinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, true);
 chatLayoutManager.setStackFromEnd(true);
当消息开始溢出底部的
RecyclerView
时,需要将当前位置设置为0,要实现这一点,一个简单的界面就像一个符咒,在
RecyclerView.Adapter
呈现最后一条消息时发出警告

public interface OnLastItemRendered{

    void lastItemRendered();

}
将接口实现从活动/片段传递到de
RecyclerView.Adapter
以调用
scrollToPosition
中的
RecyclerView

new ChatMessageListAdapter(
    context, user, lastOldMessages,
    new OnLastItemRendered() {

        @Override
        public void lastItemRendered() {

            ContainerFragment.this.myRecyclerViewToChat.scrollToPosition(0);
    }
});
最后,如果希望在
RecyclerView
中执行动画,则实现一种方法,将消息添加到
RecyclerView.Adapter
中,并在
notifyItemInserted
方法末尾调用

public void add(ChatMessage chatMessage) {

    if (this.chatMessages == null) {

        this.chatMessages = new ArrayList<ChatMessage>();
    }

    this.chatMessages.add(chatMessage);

    Collections.sort(this.chatMessages, this.orderChatMessagesComparator);

    this.notifyItemInserted(0);

    this.onLastItemRendered.lastItemRendered();
}

工作完成。

请检查:我在该链接中尝试了解决方案,但对我无效!!!无论如何,谢谢…你能详细解释一下吗?我是android开发者的初学者。实际上我使用的是recycler视图,它有聊天列表和一个带有发送按钮的编辑文本。当我输入信息时,键盘会隐藏聊天记录,因此这些聊天记录会自动滚动到键盘上方…只需在您输入文字信息的编辑文本上设置FocusChangeListener即可。比如:editText.setonFoucChangeListener(focuseListener);然后使用上面的代码来检测editText上的焦点。每次触摸editText时,它都会获得焦点,并且每次在上面的代码中调用focusListener时。因此,调用recyclerView.ScrollToPosition(sizeOfYourList);方法。它将引导您进入列表的最后一行。类似地,请调用单击“发送”按钮的这种方法。谢谢。。。代码的第一部分部分部分满足了我的期望,即当我开始键入最后看到的消息时,它会出现在键盘上方,直到这一切都很好。但我也需要当我发送信息时,它也应该出现在最后看到的信息中,这样它就像聊天应用程序一样,就像我们发送whatsapp或其他社交信息应用程序一样。第二部分就是为了这个目的。您如何将邮件添加到列表中?你能出示密码吗@维古