针对键盘弹出/消失及聊天工具的显示与消失时recyclerView内容滚动的问题
效果类似手机QQ,保持消息内容相对位置不变
mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
private int flagPosition;//the position of last visible item
private int flagTop;//the distance between the last visible item and bottom
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
int dy = oldBottom - bottom;
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
if (dy > 0) {
mRecyclerView.scrollBy(0, dy);
flagPosition = layoutManager.findLastVisibleItemPosition();
View view = layoutManager.findViewByPosition(flagPosition);
if (view != null) {
flagTop = view.getTop() - bottom;
}
} else {
View view = layoutManager.findViewByPosition(flagPosition);
if (view != null) {
mRecyclerView.scrollBy(0, view.getTop() - bottom - flagTop);
} else {
mRecyclerView.scrollBy(0, dy);
}
}
}
});