Get RecyclerView last visible item
Asked Answered
F

0

1

findLastVisibleItemPosition returns -1 with the following sequence:

onViewCreated() {
    recyclerView.setAdapter(adapter = new Adapter());
    adapter.updateContent(List<Content>); // will call notifyItemRangeChanged
    post(new Runnable() {
        run() {
            layoutManager.getLastVisibleItem(); // == -1 (using runnable or not)
        }
    }
}

It seems that the children views are not ready yet. Using a tree observer on the RecyclerView seems to solve the problem, but what would be the best approach to get the last visible item after setting content to the adapter right after creation?

Floriaflorian answered 25/8, 2015 at 0:15 Comment(5)
Maybe you can try. recyclerView.scrollToPosition(contentList.size() - 1);Chatter
does postDelayed help? you can also try to get the visibleItems in the onViewStateRestored( ) method.Winker
did you set the layout manager? recyclervew.setLayoutManager(layoutManager)Prosopopoeia
@AngelKoh: yes, adding delay (1s) helps.Floriaflorian
@Modge: yes, it's set.Floriaflorian

© 2022 - 2024 — McMap. All rights reserved.