android-打开键盘时,Recyclerview不会滚动到结尾
我在应用程序中使用recylerview,并且每当将新元素添加到recyclerview时,它都会通过使用滚动到最后一个元素
recyclerView.scrollToPosition(adapter.getCount());
但是,每当键盘打开时(由于editTextView),它都会调整视图的大小,并且recyclerview会变小,但是无法滚动到最后一个元素。
android:windowSoftInputMode="adjustResize"
我什至尝试使用以下代码滚动到最后一个元素,但是没有用
editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
recyclerView.scrollToPosition(chatAdapter.getItemCount());
}
}
});
我可以尝试adjustPan向上移动平移锅,但是它隐藏了我的工具栏。请提出任何纠正问题的方法。
10个解决方案
62 votes
您可以使用recyclerview.addOnLayoutChangeListener()捕捉键盘变化。如果底部小于oldBottom,则键盘处于向上状态。
if ( bottom < oldBottom) {
recyclerview.postDelayed(new Runnable() {
@Override
public void run() {
recyclerview.scrollToPosition(bottomPosition);
}
}, 100);
}
jihoon kim answered 2020-02-14T05:09:07Z
50 votes
将此添加到您的活动或片段中:
if (Build.VERSION.SDK_INT >= 11) {
recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v,
int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (bottom < oldBottom) {
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(
recyclerView.getAdapter().getItemCount() - 1);
}
}, 100);
}
}
});
}
mutkan answered 2020-02-14T05:09:28Z
19 votes
这个对我有用
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
anisart answered 2020-02-14T05:09:48Z
7 votes
尽管这是一个老问题,但我今天遇到了这个问题,但发现上述方法均无效。 这是我的解决方案
recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v,
int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (bottom < oldBottom) {
final int lastAdapterItem = mAdapter.getItemCount() - 1;
recyclerView.post(new Runnable() {
@Override
public void run() {
int recyclerViewPositionOffset = -1000000;
View bottomView = linearLayoutManager.findViewByPosition(lastAdapterItem);
if (bottomView != null) {
recyclerViewPositionOffset = 0 - bottomView.getHeight();
}
linearLayoutManager.scrollToPositionWithOffset(lastAdapterItem, recyclerViewPositionOffset);
}
});
}
});
}
Kutae Shaw answered 2020-02-14T05:10:08Z
6 votes
我发现postDelayed是不必要的,并且当回收站滚动到项目中间的某个位置时,使用适配器位置并不能解决问题。 我达到了想要的外观:
recycler.addOnLayoutChangeListener((view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
if (bottom < oldBottom) {
messageRecycler.scrollBy(0, oldBottom - bottom);
}
})
mp501 answered 2020-02-14T05:12:18Z
3 votes
这可行。
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mManager;
...
mManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mManager);
(initialize and set adapter.)
mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (bottom < oldBottom) scrollToBottom();
}
});
private void scrollToBottom() {
mManager.smoothScrollToPosition(mRecyclerView, null, mAdapter.getItemCount());
}
wonsuc answered 2020-02-14T05:12:38Z
2 votes
recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount());
sdelvalle57 answered 2020-02-14T05:12:55Z
1 votes
在支持库版本27.0.1中可以正常工作
清单中没有任何内容。
val currentScrollPosition = 0
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
currentScrollPosition = recyclerView.computeVerticalScrollOffset() + recyclerView.computeVerticalScrollExtent()
}
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) { }
})
storyList.addOnLayoutChangeListener { view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
if (bottom < oldBottom) {
if (currentScrollPosition >= recyclerView.computeVerticalScrollRange()) {
recyclerVIew.post {
recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_NEVER
recyclerView.smoothScrollBy(0, recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollOffset() + recyclerView.computeVerticalScrollExtent())
}
}
} else {
recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_ALWAYS
}
}
minjung Ahn answered 2020-02-14T05:13:21Z
0 votes
将RecyclerView绑定到NestedScrollView中
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Karthikeyan answered 2020-02-14T05:16:38Z
0 votes
layoutManager.scrollToPositionWithOffset(chatMessageAdapter.getItemCount()-1,0);
Ramesh R answered 2020-02-14T05:16:58Z