Android RecyclerView 滚动条设置:置顶显示
您可以在RecyclerView的XML布局文件中添加一个FrameLayout,将RecyclerView放在它的下层,然后将ScrollBar放在它的上层,如下所示:
<FrameLayout
android:layout_width='match_parent'
android:layout_height='match_parent'>
<androidx.recyclerview.widget.RecyclerView
android:id='+@id/recyclerView'
android:layout_width='match_parent'
android:layout_height='match_parent' />
<androidx.recyclerview.widget.RecyclerView
android:id='+@id/scrollBar'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:layout_gravity='top' />
</FrameLayout>
在RecyclerView的代码中,您可以使用addOnScrollListener方法监听RecyclerView的滚动事件,并使用scrollBar的方法将它与RecyclerView同步。例如:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrollBar.scrollToPosition(layoutManager.findFirstVisibleItemPosition());
}
});
原文地址: https://www.cveoy.top/t/topic/oPwc 著作权归作者所有。请勿转载和采集!