在RecyclerView中滚动项目,以便在显示软键盘时最后一项始终相同

时间:2016-05-15 18:25:36

标签: android android-layout android-recyclerview

我正在开发一个消息传递应用,对布局没什么好感 - 布局顶部有Toolbar,底部有EditText,列表有RecyclerView消息,填补其间的空间。

我的问题是,当点击/按下RecyclerView显示软键盘时,EditText中的最后一个可见项位置无法保留。即,如果列表有20个项目,并且当软键盘未显示时最后一个可见,则为项目12,当显示软键盘时,我仍然希望最后一个可见为项目12。

我已经在清单和代码中尝试了 adjustPan adjustResize 的组合,但没有解决问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为您最好的选择是在清单中设置adjustResize,然后创建一个覆盖RecyclerView的自定义onLayout()子类:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        super.onLayout(changed, l, t, r, b);

        int height = b - t;
        if (height != mLastHeight) {
            int last = getAdapter.getItemCount() - 1;
            scrollToPosition(last);
        }
        mLastHeight = height;
    }