How to scroll the edittext inside the scrollview

前端 未结 12 1990
南笙
南笙 2020-12-02 10:27

I have a scrollview inside which there is a editext which is multiline. I want to scroll the edittext to see the lower content but it can\'t be done.

12条回答
  •  日久生厌
    2020-12-02 11:15

    Building upon the answer by @Ayman Mahgoub and @Hariharan. The solution worked great except while scrolling the edit text there is no scroll momentum. As soon as the finger lifts up, the scrolling immediately stops.

    To gain scroll momentum wrap a scrollview around the EditText and let the EditText's height wrap content (set minHeight if you'd like). Remove the following lines from the edit text:

    android:scrollbarStyle="insideInset"
    android:scrollbars="vertical" 
    android:overScrollMode="always"
    

    Now the nested ScrollView wrapped around the EditText takes care of the scrolling. Update the onTouch handler to take into account the updated view hierarchy:

    public boolean onTouch(View view, MotionEvent event) {
        v.getParent().getParent().requestDisallowInterceptTouchEvent(true);
        switch (event.getAction() & MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_UP:
                v.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return false; 
    

    Be warned, this solution is now tightly coupled to the view hierarchy.

    Gist: https://gist.github.com/atoennis/7549fba2634d0abccddf

提交回复
热议问题