VerticalSeekbar在水平滚动 View 中无法正常工作

2019年5月2日 30点热度 0条评论

我正在尝试在水平滚动视图中实现垂直搜索栏,
但是搜索栏无法在其中顺利运行。

这是我的垂直sekkbar类..... Source仅来自stackoverflow。

public class VerticalSeekBar extends SeekBar {

    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

这就是我实现seekbar的方式。

    seekbar3.setMax(5000);
      seekbar3.setProgress(mEqualizer.getBandLevel(band));
      seekbar3.setFocusable(false);
      seekbar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        horizontalscroll.setOnTouchListener(null); 
        horizontalscroll.setEnabled(false);
        mHandler.removeCallbacks(mUpdateTimeTask);
        seekbar3.setProgress(mEqualizer.getBandLevel(band));

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        //horizontalscroll.setEnabled(false);
        /*horizontalscroll.setOnTouchListener(new View.OnTouchListener(){ 
         @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true; 
            }
        });*/
        ObjectAnimator animator=ObjectAnimator.ofInt(horizontalscroll, "scrollX",8,8 );
        animator.setDuration(2000);
        animator.start();
    //  seekbar3.setProgress(mEqualizer.getBandLevel(band));
        mHandler.removeCallbacks(mUpdateTimeTask);


        // Enable Scrolling by removing the OnTouchListner
   //   horizontalscroll.setOnTouchListener(null); 
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
     mEqualizer.setBandLevel(band, (short)(progress+min));
     seekbar3top.setText((progress+min)+"mB");
     seekbar3.setProgress(progress);
    // seekbar3.onDraw(c)
 /* horizontalscroll.setOnTouchListener(new View.OnTouchListener(){ 
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true; 
        }});*/
    }
   });

     }

谁能建议我用简单的方法做。

我的Verticalseekbar不能平稳移动。

提前感谢:)

解决方案如下:

最后我解决了这个问题
我将我的垂直搜索栏类更改为

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {
    private boolean mIsMovingThumb = false;
       static private float THUMB_SLOP = 25;
    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }
    private boolean isWithinThumb(MotionEvent event) {
                final float progress = getProgress();
                final float density = this.getResources().getDisplayMetrics().density;
                final float height = getHeight();
                final float y = event.getY();
                final float max = getMax();
                if (progress >= max - (int)(max * (y + THUMB_SLOP * density) / height)
                    && progress <= max - (int)(max * (y - THUMB_SLOP * density) / height))
                    return true;
                else
                    return false;
            }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
boolean handled=false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (isWithinThumb(event)) {
                                       getParent().requestDisallowInterceptTouchEvent(true);
                                        mIsMovingThumb = true;
                                        handled = true;
                                    }
                                    break;
            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:
                                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                                onSizeChanged(getWidth(), getHeight(), 0, 0);
                                if (mIsMovingThumb) {
                                    setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                                    onSizeChanged(getWidth(), getHeight(), 0, 0);
                                handled = true;
                                }
                                 break;
            case MotionEvent.ACTION_CANCEL:
                                getParent().requestDisallowInterceptTouchEvent(false);
                                mIsMovingThumb = false;
                             handled = true;
                                 break;

        }
        return true;
    }
}