Android:当软键盘出现时,Recyclerview不会重新调整大小

9
我有一个包含片段和自定义视图的活动。自定义视图中包含一个edittext,直接下方是一个高度为match_parent的recyclerview。当用户在edittext上获得焦点时,会出现一个软输入键盘。不幸的是,这个键盘把recyclerview的一半隐藏了起来。我希望recyclerview可以调整大小以适应屏幕上剩余可见的高度(在edittext下方),以便列表中的项目不会被键盘遮挡。
我已尝试在清单文件中将adjustresize设置为活动,但没有效果:
android:windowSoftInputMode="adjustResize"

我尝试将应用主题设置为基本主题(以防我的自定义主题导致问题)- 没有效果:

android:theme="@style/Theme.AppCompat.NoActionBar"

我已检查了整个层次结构并没有发现任何Framelayouts与需要调整大小的视图相关(我在 Stack Overflow 上读到过可能会存在问题的讨论),因此这方面问题排除。
我已检查了活动、片段、自定义视图和每个连接的适配器,以确保没有在任何地方调用getWindow(我没有调用)。
自定义视图是在运行时从ViewStub中填充的。这是否有关联?
编辑:
父元素的内容:
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
...


    <ViewStub
        android:id="@+id/viewstub_outername"
        android:layout="@layout/viewstub_thatIneed"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

视图存根的内容:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewstub_root"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@android:color/white"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:visibility="invisible"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:visibility="invisible"
        />

</RelativeLayout>

如下所示显示键盘:
public static void showSoftKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}

请贴出能够演示问题的代码片段。 - Ganesh Kumar
@GaneshKumar,请看我的编辑。 - Jon
1
嘿@Jon,你解决了这个问题吗?能否指导我如何修复它? - Haresh Chaudhary
我也遇到了同样的问题。 - Abhi Muktheeswarar
2个回答

16

不要在清单文件中做任何事情。只需使用。

((LinearLayoutManager)recyclerView.getLayoutManager()).setStackFromEnd(true);

这将处理recyclerview的调整大小。


1
你的解决方案解决了我的问题,其他的解决方案没有解决我的问题。谢谢! - Mu Sa
1
惊人的解决方案。尝试过ADJUST_RESIZEHIDDEN等方法,但都失败了,这个解决方案拯救了一天。安卓系统从来不会让我失望,哈哈 :D - Jian Guo
3
你是我的神。谢谢! - A.Mamode
当顶部有一个EditText时,这会破坏RecycelrView的布局。Recylerview可以滚动,但视图是不可见的。 - Raghunandan

0
window.decorView.setOnApplyWindowInsetsListener { view, insets ->
    val insetsCompat = toWindowInsetsCompat(insets, view)
    mainNavigation.isGone = insetsCompat.isVisible(ime())
    view.onApplyWindowInsets(insets)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接