1

我已经被困了很长一段时间了。我正在尝试开发一个聊天模块。我一直被困在这部分,当SoftInputKeyboard覆盖RecyclerView. 我已经尝试了几乎所有的组合和adjustResize没有成功。adjustPanstateHiddenstateVisible

在使用2-3 项时adjustPan隐藏。我附上了截图。任何帮助都将不胜感激。ActionBarrecyclerview

XML

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_grey"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true">

<LinearLayout
    android:id="@+id/listFooter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="8dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/messageInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:hint="Write a message"
        android:inputType="textMultiLine"
        android:padding="16dp"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:contentDescription="@null"
        android:padding="12dp"
        android:src="@drawable/ic_send" />
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_chat_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/listFooter"
    android:layout_marginTop="8dp" />
</RelativeLayout>
  • A ) 调整大小

使用调整大小

  • B ) 使用 AdjustPan

在此处输入图像描述

4

2 回答 2

1

显示/隐藏键盘时,android:windowSoftInputMode不会为您滚动内容。

文档说:

adjustResize - Activity 的主窗口总是调整大小,以便为屏幕上的软键盘腾出空间。

adjustPan - Activity 的主窗口没有调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。

基本上这意味着adjustResize让你的 rootview 更小,并将 softKeyboard 放在它下面。并将adjustPan根视图的上半部分推出屏幕,为软键盘腾出空间。

我建议使用adjustResize,因为它不会将您的工具栏推出屏幕。然后你必须自己滚动内容。显然说起来容易做起来难,但是有内置的方法可以做到这一点。

首先,您必须在回收站视图中获取最后一个可见项目的位置。

//class variable
private int lastVisiblePosition = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    //...
    recyclerview_chat_main.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            super.onScrolled(recyclerView, dx, dy);
            lastVisiblePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastVisibleItemPosition();
        }
    });
    //...
}

然后你要做的是在显示软键盘时滚动到该项目,问题是没有内置的方法来显示键盘,幸运的是有人已经在这里解决了这个问题:https ://stackoverflow.com /a/25681196/2027232

使用 Jaap 的答案,我们可以添加如下内容:

//class variables
private ViewGroup rootLayout = null;
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    //...
    ViewGroup rootLayout = (ViewGroup)findViewById(android.R.id.content);
    keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            int heightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
            int contentViewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();

            if(heightDiff > contentViewTop)
            {
                recyclerview_chat_main.getLayoutManager().scrollToPosition(lastVisiblePosition);
            }
        }
    };
    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
    //...
}

最后不要忘记在活动被破坏时删除全局监听器:

@Override
protected void onDestroy()
{
    super.onDestroy();
    if(rootLayout != null && keyboardLayoutListener != null)
        rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(keyboardLayoutListener);
}
于 2017-03-14T12:21:52.983 回答
1

接受的答案对我不起作用。这有效:

在清单中的活动标记中,添加android:windowSoftInputMode="adjustResize"

在活动的onCreate方法中添加:

View.OnLayoutChangeListener layoutChangeListener = new View.OnLayoutChangeListener()
{
    @Override
    public void onLayoutChange(View v, int left, final int top, int right, final int bottom,
                        int oldLeft, final int oldTop, int oldRight, final int oldBottom)
    {
        if (oldBottom != 0)
        {
            //when softkeyboard opens, the height of layout get's small, and when softkeyboa
            //-rd closes the height grows back(gets larger).We can find the change of height
            // by doing oldBotton - bottom, and the result of subtraction is how much we nee
            //-d to scroll. Change of height is positive if keyboard is opened, and negative
            //if it's closed.
            int pixelsToScrollVertically = oldBottom - bottom;
            recyclerView.scrollBy(0, pixelsToScrollVertically);
        }
    }
};


myAdapter = new MyAdapter();
recyclerView.setAdapter(myAdapter);

recyclerView.addOnLayoutChangeListener(layoutChangeListener);
于 2020-04-06T11:25:17.397 回答