I use SortedList in RecyclerView Adapter and I'd like to know when RecyclerView has finished its update after I change data in Adapter (and called proper method e.g. notifyItemRangeChanged). Is there any way to do this?
My problem is that I need to scroll RecyclerView to the top after filtering its content. I call from Activity method on my Adapter which filter items on its member. After that I just call scrollToPosition(0) on RecyclerView and its not always work as expected, especially when after changes operation on list is only 1 item.
Here is code of update method I call on my adapter:
private SortedList<Game> games;
private ArrayList<Game> allGames;
public void search(String query) {
replaceAll(filterGames(query));
}
public void replaceAll(Collection<Game> games) {
this.games.beginBatchedUpdates();
List<Game> gamesToRemove = new ArrayList<>();
for (int i = 0; i < this.games.size(); i++) {
Game game = this.games.get(i);
if (!games.contains(game)) {
gamesToRemove.add(game);
}
}
for (Game game : gamesToRemove) {
this.games.remove(game);
}
this.games.addAll(games);
this.games.endBatchedUpdates();
}
private Collection<Game> filterGames(String query) {
query = query.toLowerCase();
List<Game> filteredGames = new ArrayList<>();
for (Game game : allGames) {
String gameTitle = game.getTitle().toLowerCase();
if (gameTitle.contains(query)) {
filteredGames.add(game);
}
}
return filteredGames;
}
And here how I call it from Activity:
private RecyclerView gamesList;
private GameAdapter gamesAdapter;
@Override
public boolean onQueryTextChange(String newText) {
gamesAdapter.search(newText);
gamesList.scrollToPosition(0);
return false;
}