键盘打开时,Recyclerview不会滚动到结尾 [英] Recyclerview not scrolling to end when keyboard opens
问题描述
我在应用程序中使用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()
捕捉键盘上的更改.
如果bottom小于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屋!