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

前端 未结 6 1758
逝去的感伤
逝去的感伤 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:08

    by override RecyclerView onScrolled(int dx,int dy) , you can get the scroll offset. but when you add or remove the item, the value may be wrong.

    public class TempRecyclerView extends RecyclerView {
       private int offsetX = 0;
       private int offsetY = 0;
       public TempRecyclerView(Context context) {
           super(context);
       }
    
       public TempRecyclerView(Context context, @Nullable AttributeSet attrs) {
          super(context, attrs);
       }
    
       public TempRecyclerView(Context context, @Nullable AttributeSet attrs,int defStyle){
          super(context, attrs, defStyle);
       }
    /**
     * Called when the scroll position of this RecyclerView changes. Subclasses 
       should usethis method to respond to scrolling within the adapter's data 
       set instead of an explicit listener.
     * 

    This method will always be invoked before listeners. If a subclass needs to perform any additional upkeep or bookkeeping after scrolling but before listeners run, this is a good place to do so.

    */ @Override public void onScrolled(int dx, int dy) { // super.onScrolled(dx, dy); offsetX+=dx; offsetY+=dy; } }

提交回复
热议问题