RecyclerView 滚动到顶部
要将RecyclerView滚动到顶部,可以使用以下代码:
- 使用LayoutManager的scrollToPosition()方法滚动到列表的第一个位置。
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.scrollToPosition(0);
- 如果你想要平滑滚动到顶部,可以使用RecyclerView的smoothScrollToPosition()方法。
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.smoothScrollToPosition(0);
这样就可以将RecyclerView滚动到顶部了
原文地址: http://www.cveoy.top/t/topic/ilCZ 著作权归作者所有。请勿转载和采集!