RecyclerView scroll to position when a new item is added

后端 未结 7 1692
南笙
南笙 2020-12-31 03:18

I want my RecyclerView to scroll to the bottom when a new item is added to the list. Below is my code:

RecyclerView.LayoutManager layoutManager = new LinearL         


        
7条回答
  •  滥情空心
    2020-12-31 04:03

    ישו אוהב אותך's solution worked on other projects. But setting recyclerView.scrollToPosition() didn't work for me. This is what finally did the job:

    public void addNewCard() {
        data.add("New");
        adapter.notifyItemInserted(data.size());
    
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0,buttonAddNewItem.getTop());
            }
        });
    }
    

提交回复
热议问题