Android seekbar how to block dragging / disable it for short time

为君一笑 提交于 2019-12-04 10:11:42

问题


I want to block the dragging of a seekbar if a value is true. Is this even possible? I use a seekbar as a switch. If my seekbar value is 0, and I press the button action A started. If my seekbar value is 1 and I press the button action B is started.
I want to prevent that If action A or B is running the seekbar is dragged to another position.

How can I achieve this?


回答1:


You should set your own onTouchListener and just return true.

seekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });



回答2:


Have you tried disabling the view?

Ref: http://developer.android.com/reference/android/view/View.html#setEnabled(boolean)

Update:

https://stackoverflow.com/a/3278616/529691

Ideally you shouldn't be doing this, as this is going to confuse the user. My suggestion is rethink your approach.




回答3:


Set one class level variable

private boolean blockSeekBar = false;

after that set blockSeekBar variable as per your requirement and use in app.

seekBar.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return blockSeekbar;
        }
    });

setEnabled(true/false) also choice but there view is not showing good so thats why above code is perfectly working.




回答4:


int lastProgress=0;
  1. Keep track of the last progress when the Seekbar.OnSeekbarChangeListener is fired.

    private SeekBar.OnSeekBarChangeListener mOnSeekbarChangeListener = new 
    
    SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
        if (fromUser) {
            if (true) {
                seekbar.setProgress(lastProgress);
            } else {
                seekbar.setProgress(progress);
            }
        }
    }
    
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        lastProgress = seekBar.getProgress();
    }
    
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    
    }
    };
    


来源:https://stackoverflow.com/questions/8872082/android-seekbar-how-to-block-dragging-disable-it-for-short-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!