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

时间:2015-09-23 17:09:53

标签: java android android-layout horizontalscrollview android-vertical-seekbar

我试图在水平滚动视图中实现垂直搜索栏,
但是搜索栏不能顺利运行。

这是我的垂直sekkbar类......仅来自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;
    }
}

这是我实施搜索栏的方式。

    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不会像它应该移动那样平滑移动。 Thanx提前:))

1 个答案:

答案 0 :(得分:0)

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

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;
    }
}