cding24 / android-wheel

Automatically exported from code.google.com/p/android-wheel
0 stars 0 forks source link

Click on number centres it #7

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is more a request for enhancement, with the iPhone equivalent (yes I know 
android is not iPhone, but we can always 'steal' some of their usability 
ideas), in a number picker (could be any item though), if you click on a number 
that is not in the middle, that number is then pushed to the middle (as apposed 
to just dragging it to the middle). Its a neat enhancement. 

I didnt want to alter the core code to make upgrades possible in the future so 
I extended the class with the below, but you could potentially add the 
'(getHeight()/2) - event.getY())' bit into your action_up with some invalidates 
and it would be done. Its your code so your call, just a sugestion. Anyway, 
here is the important bits of the overriding class ...

public class TouchWheelView extends WheelView {
    private boolean isScrollingPerformed; 
    private float clickDownY = 0.0F ;

    /**
     * Constructor
     */
    public TouchWheelView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Constructor
     */
    public TouchWheelView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Constructor
     */
    public TouchWheelView(Context context) {
        super(context);
        this.onFinishInflate();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        // Add listeners to emulate the isScrollingPerformed behaviour
        this.addScrollingListener(new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
                isScrollingPerformed = true;

            }
            public void onScrollingFinished(WheelView wheel) {
                isScrollingPerformed = false;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        WheelAdapter adapter = getAdapter();
        if (adapter == null) {
            return true;
        }
        // for my event, if an item is click (when button goes up), if click is outside focus region, emulate a move
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            clickDownY = event.getY();
            isScrollingPerformed = false ; 
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            isScrollingPerformed = true ; 
            break;
        }
        case MotionEvent.ACTION_UP:
            if (!isScrollingPerformed) {
                // Pretend to to a move
                event.setAction(MotionEvent.ACTION_MOVE);
                event.setLocation(event.getX(),  clickDownY + ((getHeight()/2) - event.getY()));
                super.onTouchEvent(event);
                // Now reset back to what it was
                event.setAction(MotionEvent.ACTION_UP);
                event.setLocation(event.getX(), (getHeight()/2));
            }
            break;          
        }
        return super.onTouchEvent(event);
    }
}

Original issue reported on code.google.com by davidhil...@gmail.com on 5 Dec 2010 at 1:01

GoogleCodeExporter commented 8 years ago
Thank you!
Will add this feature. It is really useful thing :)

Original comment by yuri.kan...@gmail.com on 25 Dec 2010 at 7:21

GoogleCodeExporter commented 8 years ago
Should work now

Original comment by yuri.kan...@gmail.com on 27 Jan 2011 at 8:57