我正在练习构建一个聊天应用程序。当我插入一个项目到recyclerview时,我需要它滚动到底部。我的recyclerview代码现在看起来是这样的。我尝试添加一个onScroll侦听器,但这种方法是错误的。谢谢!
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
((SimpleItemAnimator) binding.chatRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
linearLayoutManager.setReverseLayout(true);
binding.chatRecyclerView.setLayoutManager(linearLayoutManager);
binding.chatRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
Log.i(TAG, "onScrolled: " + firstVisiblePosition);
if (dy < 0) {
binding.chatDate.setVisibility(View.VISIBLE);
binding.chatDate.setText(sfdMainDate.format(new Date(chatadapter.getItem(firstVisiblePosition).getTimestamp())));
} else if (dy > 0) {
binding.chatDate.setVisibility(View.GONE);
}
}
});```
1条答案
按热度按时间mcvgt66p1#
将数据观察器添加到适配器并重写onItemRangeInserted。