Seekbar's thumb on click

你。 提交于 2019-12-01 16:00:15

Combining zwebie and Nermeens answers to the proper solution:

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    private int mProgressAtStartTracking;
    private final int SENSITIVITY;

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        // handle progress change
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        mProgressAtStartTracking = seekBar.getProgress();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(Math.abs(mProgressAtStartTracking - seekBar.getProgress()) <= SENSITIVITY){
            // react to thumb click
        }
    }
});

So this really fires only on thumb click, not on thumb move and not on click on other parts of the seekbar.

You can adjust sensitivity, because sometimes a click already moves the thumb a little bit and allowing little changes while still clicking is less frustrating. A good value here depends on the size of the seekbar and the max value it can have. For me 3 worked well on a full-width seekbar with 50 max on a portrait layout.

I was able to achieve this in the following way:

seekBar.setOnTouchListener(new OnTouchListener() 
{
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            changedPosition = true;
            seekBar.setProgress(seekBar.getProgress());
            return false;
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {
            if(!changedPosition)
            {
                  //do action here
            }
        }
   }

hope this helps

I used Bijom's answer as inspiration. This will fire when the thumb moves so if the user doesn't click on the thumb it won't move. Hope this helps. :)

If you have any questions about how it works feel free to comment.

    mSeekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = mSeekBarSpeed.getProgress();

        boolean started = false; //use this variable to see whether the user clicked the right place


        @Override
        public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
            if(!started){ //check to see if user clicks the right place
                //if the user clicks within a specific threshold
                float threshold = (float)seekBar.getMax() / seekBar.getWidth() * seekBar.getThumb().getIntrinsicWidth() / 2;
                if(Math.abs(progressValue - progress) < threshold){
                     if(fromUser){ //checks if user actually clicked it
                         started = true;
                     }

                }else{
                    seekBar.setProgress(progress); //set to original progress
                    onStopTrackingTouch(seekBar); //not really necessary, keep or delete based on your needs
                    return; //get out of method
                }
            }

            if(started) {

                progress = progressValue; //update progress variable
                System.out.println("onProgressChanged:" + progress + "/" + seekBar.getMax());

                //DO WHAT YOU NEED TO DO WHEN PROGRESS IS CHANGING

            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            System.out.println("onStartTracking:" + progress + "/" + seekBar.getMax());
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            System.out.println("onStopTracking:" + progress + "/" + seekBar.getMax());
            //DO WHATEVER YOU NEED TO DO WHEN PROGRESS IS DONE CHANGING

            started = false; //remember to set variable to false
        }

    });

I do it this way:

pagesSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //listener
        @Override
        public void onStopTrackingTouch(final SeekBar seekBar) {
            //add your event here
        }

        @Override
        public void onStartTrackingTouch(final SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) {
                updateControls(progress, false);
        }
    });
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!