如果您查看AsyncListDiffer
's的 javadocs submitList
,您会注意到第二个参数是Runnable
在项目提交给适配器之后执行的 a:
/**
* Pass a new List to the AdapterHelper. Adapter updates will be computed on a background
* thread.
* <p>
* If a List is already present, a diff will be computed asynchronously on a background thread.
* When the diff is computed, it will be applied (dispatched to the {@link ListUpdateCallback}),
* and the new List will be swapped in.
* <p>
* The commit callback can be used to know when the List is committed, but note that it
* may not be executed. If List B is submitted immediately after List A, and is
* committed directly, the callback associated with List A will not be run.
*
* @param newList The new List.
* @param commitCallback Optional runnable that is executed when the List is committed, if
* it is committed.
*/
所以你想要的是这个(顺便说一句,这是在 Kotlin 中):
adapter.submitList(items) {
// This will run after items have been set in the adapter
recyclerView.scrollToPosition(0)
}
或在 Java 中
adapter.submitList(items, () -> {
recyclerView.scrollToPosition(0);
});