我在RecyclerView适配器中使用SortedList,我想知道RecyclerView在更改Adapter中的数据(并调用适当的方法,例如notifyItemRangeChanged)后何时完成更新。有没有办法做到这一点?如何找出RecyclerView已完成更新?
我的问题是,我需要在过滤其内容后将RecyclerView滚动到顶部。我从我的适配器上的Activity方法调用了其成员上的项目。之后,我只是在RecyclerView上调用scrollToPosition(0),它并不总是像预期的那样工作,特别是当列表上的更改操作只有一个项目时。
以下是更新方法的代码,我在我的适配器上呼吁:
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;
}
在这里,我怎么称呼它的活动:
private RecyclerView gamesList;
private GameAdapter gamesAdapter;
@Override
public boolean onQueryTextChange(String newText) {
gamesAdapter.search(newText);
gamesList.scrollToPosition(0);
return false;
}
但是,您需要什么?什么是用例? – azizbekian
我想在添加/删除项目后将RecyclerView滚动到顶部。当我在适配器上开始更新时尝试滚动时,出现问题,因为项目仍在处理中。 – ostojan
'因为项目仍在处理中'如果它们不在屏幕上,它们如何“被处理”,因此它们从窗口中分离出来? – azizbekian