简体   繁体   English

键盘弹出时如何上推视图的内容?

[英]How to push the contents of a View up when Keyboard pops up?

I have a RecyclerView and an EditText inside a CardView . 我在CardView有一个RecyclerView和一个EditText Purpose of the activity is to chat. 活动的目的是聊天。 So the RecyclerView contains all the messages sent and received, and the CardView is where the user types the messages. 因此, RecyclerView包含所有已发送和接收的消息,而CardView是用户键入消息的地方。 Coming to the problem, I need to move the contents up when the Keyboard pops up. 出现问题时,我需要在键盘弹出时将内容上移。 Now, this doesn't seems to be a simple problem. 现在,这似乎不是一个简单的问题。

I tried all these Questions: 我尝试了所有这些问题:

And, the solution to most comes down to altering android:windowSoftInputMode . 而且,大多数解决方案归结为更改android:windowSoftInputMode Which I tried, switching to adjustPan and adjustResize . 我尝试过,切换到adjustPanadjustResize Here is the code to my Activity: 这是我的活动的代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F0F0F0"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingStart="10dp"
    android:paddingEnd="10dp"
    tools:context="com.devpost.airway.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/chatView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.CardView
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dp"
        app:cardElevation="0dp"
        app:cardCornerRadius="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <EditText
            android:id="@+id/chat_input"
            android:paddingStart="2dp"
            android:paddingEnd="2dp"
            android:inputType="text"
            android:imeOptions="actionSend"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>

</RelativeLayout>

And adjustPan does work in a very vague way, I've added the screenshot below, kindly take a look : adjustPan确实以一种非常模糊的方式工作,我在下面添加了屏幕截图,请看一下:

IMAGE-1 图像1

图像1

IMAGE-2 影像2

在此处输入图片说明

Now, clearly from Image-1 and Image-2, there are few issues: 现在,从Image-1和Image-2可以清楚地看到几个问题:

  • The ActionBar is moved up . ActionBar向上移动。
  • When Keyboard pops up it adds a transition animation 弹出键盘时,会添加过渡动画
  • It pushes the entire RecyclerView up 它将整个RecyclerView向上推

So, the question is: 因此, 问题是:

Is it manually possible to override any methods and implement things myself up, other than allowing the android:windowSoftInputMode to change the way it prefers? 除了允许android:windowSoftInputMode更改其喜欢的方式之外,是否可以手动覆盖任何方法并自己实现事情? Kindly help. 请帮助。

<android.support.v7.widget.RecyclerView
    android:id="@+id/chatView"
    android:layout_width="match_parent"
    android:layout_above="@+id/editorCard"
    android:layout_height="wrap_content" />

<android.support.v7.widget.CardView
    android:id="@+id/editorCard"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="10dp"
    app:cardElevation="0dp"
    app:cardCornerRadius="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="50dp">
 <!--your editor code-->
 </android.support.v7.widget.CardView>

wrap_content and layout_above are the key changes for the desired behavior wrap_contentlayout_above是所需行为的关键更改

and in softinputmethod , make it adjustPan|adjustResize 然后在softinputmethod ,使其为adjustPan|adjustResize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM