Shift recyclerview focus to last element in the li

2020-06-30 05:07发布

I am using recyclerview to display messages in a chat window. However, when a new message is being edited, the recycler view focus should be on the last element. I expect there would be some way to shift the focus of a recyclerview to last element instead of first.

8条回答
戒情不戒烟
2楼-- · 2020-06-30 06:02

Use

recyclerView.smoothScrollToPosition(position)
adapter.notifyDataSetChanged();

where position is the index of last element

recyclerView.getAdapter().getItemCount()-1

use this to get last element.

This worked for me .

查看更多
ゆ 、 Hurt°
3楼-- · 2020-06-30 06:03

use layoutManager.setStackFromEnd(true); instead of this.

Sample

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    RecyclerView recyclerView = findViewById(R.id.list);
    layoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(layoutManager);
查看更多
登录 后发表回答