Detecting thumb position in SeekBar prior to API version 16

后端 未结 4 1999
清酒与你
清酒与你 2021-02-07 11:11

Basically, I need to detect when the progress changes in the SeekBar and draw a text view on top of the thumb indicating the progress value.

I do this by implementing a

4条回答
  •  悲&欢浪女
    2021-02-07 12:01

    @Sherif elKhatib's answer is great but has the disadvantage of caching a copy of the thumb even on API>=16. I've improved it so that it only caches the Thumb Drawable on API<=15 plus it overrides the method in SeekBar.java to avoid having two methods do the same on API>=16. Only downside: It needs target SDK to be >= 16 which should be the case in most apps nowadays.

    public class ThumbSeekBar extends AppCompatSeekBar {
    
        private Drawable thumb;
    
        public ThumbSeekBar(Context context) {
            super(context);
        }
    
        public ThumbSeekBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ThumbSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public Drawable getThumb() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                return super.getThumb();
            }
            return thumb;
        }
    
        @Override
        public void setThumb(Drawable thumb) {
            super.setThumb(thumb);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                this.thumb = thumb;
            }
        }
    }
    

提交回复
热议问题