Search code examples
androidandroid-edittextscrollviewtouch-eventmotionevent

ScrollView get back to top


So i have scrollView that has only one view on it (EditText), and i want to implement the

dispatchTouchEvent(MotionEvent me)
 View current_view = Processor.findViewGroupWithPoint(
            (int) me.getRawX(), (int) me.getRawY(), rmodel.layout.parent);
    if (current_view instanceof ScrollView) {


        switch( me.getAction() ){
        case MotionEvent.ACTION_DOWN:
            Log.d(TAG, " down");

            ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_DOWN);

            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, " up");
((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
            break;
        }

}

My problem is, when i scroll down, it works, but i have to hold my finger on the screen, as if i removed it, it will get back to top of the scrollView.


Solution

  • As far as I can tell it goes to the top of the scrollView when you let go because you tell it to. you say

    case MotionEvent.ACTION_UP:
        Log.d(TAG, " up");
        ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
    

    MotionEvent.ACTION_UP is used to detect when your finger is released, so when it does in this you tell it to go to the top with

    ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
    

    To fix this, all you really have to do is remove that line...

    EDIT:

    I wrote this up and it should work, might need some tweaks.

    boolean bottom = false;
    

    Basically here just create a boolean called bottom and set it to false ^^

    case MotionEvent.ACTION_DOWN:
        Log.d(TAG, " down");
    
        if (bottom = false){
            ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_DOWN);
        }else{
            ((ScrollView) findViewById(111111)).fullScroll(View.FOCUS_UP);
        }
        break;
    }
    

    This is just seeing if the ScrollView is at the bottom or now... Hope it works!