键盘打开时 Recyclerview 不会滚动到结束 [英] Recyclerview not scrolling to end when keyboard opens

查看:27
本文介绍了键盘打开时 Recyclerview 不会滚动到结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了 recylerview,每当新元素添加到 recyclerview 时,它都会使用

I am using recylerview in my application and whenever new element is added to recyclerview, it scrolls to last element by using

recyclerView.scrollToPosition(adapter.getCount());

但是,每当键盘打开时(由于editTextView),它会调整视图的大小并且recyclerview变小,但无法滚动到最后一个元素.

But, whenever keyboard opens(because of editTextView), it resizes the view and recyclerview gets smaller, but couldn't scroll to last element.

android:windowSoftInputMode="adjustResize"

我什至尝试使用以下代码滚动到最后一个元素,但没有用

I even tried to used the following code to scroll to last element, but it didn't work

editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                recyclerView.scrollToPosition(chatAdapter.getItemCount());
            }
        }
    });

我可以尝试 adjustPan 向上移动平移,但它隐藏了我的工具栏.请提出任何纠正问题的方法.

I can try adjustPan to shift the pan up, but it is hiding my toolbar. Please suggest any way to rectify the issue.

推荐答案

您可以使用 recyclerview.addOnLayoutChangeListener() 来捕捉键盘的变化.如果底部小于 oldBottom 则键盘处于向上状态.

You can catch keyboard up changes using recyclerview.addOnLayoutChangeListener(). If bottom is smaller than oldBottom then keyboard is in up state.

if ( bottom < oldBottom) { 
   recyclerview.postDelayed(new Runnable() { 
       @Override 
       public void run() {
           recyclerview.smoothScrollToPosition(bottomPosition); 
       }
    }, 100);
}

这篇关于键盘打开时 Recyclerview 不会滚动到结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆