Android: How to get the current X offset of RecyclerView?

前端 未结 6 1667
逝去的感伤
逝去的感伤 2020-12-02 07:48

I\'m using a Scrollview for an infinite \"Time Picker Carousel\" and found out, that it is not the best approach (last question)

Now, I found the Recycl

6条回答
  •  有刺的猬
    2020-12-02 08:06

    Thanks to @umar-qureshi for the right lead! It appears that you can determine the scroll percentage with Offset, Extent, and Range such that

    percentage = 100 * offset / (range - extent)
    

    For example (to be put in an OnScrollListener):

    int offset = recyclerView.computeVerticalScrollOffset();
    int extent = recyclerView.computeVerticalScrollExtent();
    int range = recyclerView.computeVerticalScrollRange();
    
    int percentage = (int)(100.0 * offset / (float)(range - extent));
    
    Log.i("RecyclerView, "scroll percentage: "+ percentage + "%");
    

提交回复
热议问题