Android getting exact scroll position in ListView

前端 未结 8 1246
误落风尘
误落风尘 2020-12-01 02:56

I\'d like to get the exact, pixel position of the ListView scroll. And no, I am not referring to the first visible position.

Is there a way to achieve this?

8条回答
  •  误落风尘
    2020-12-01 03:34

    Simplest idea I could come up with was to extend ListView and expose the "computeVerticalScrollOffset" which is protected by default, then use "com.your.package.CustomListView" in your xml layouts.

    public class CustomListView extends ListView {
    
        public CustomListView(Context context) {
            super(context);
        }
    
        public CustomListView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public int computeVerticalScrollOffset() {
            return super.computeVerticalScrollOffset();
        }
    }
    

提交回复
热议问题