SoFunction
Updated on 2025-03-11

Perfectly resolve scroll conflict between EditText and ScrollView (Part 1)

I searched for the scroll conflict between EditText and ScrollView on the Internet and found that almost all solutions are to hand the event to EditText when touching EditText, otherwise the event will be handed over to ScrollView. This does initially resolve the rolling conflict between the two, but it is not the best solution. For example, EditText could have displayed 6 lines of text, but currently only 5 lines of text are displayed. At this time, we slide in the EditText area and expect the entire page to be scrollable. However, since we handed the event to EditText for processing, the page cannot be scrolled, which is extremely poor. In fact, we hope that when the scroll bar appears in EditText, the scroll event will be handed over to it. In other cases, ScrollView should be handled. So how to implement it? Next, let’s make a small demo to implement this solution.

1. Layout files

First, write the layout file, and you can see that this is a very simple layout: a ScrollView wraps a vertical LinearLayout, and there are two TextViews and an EditText in the LinearLayout. In order to distinguish the scope of EditText, a background rectangle_shape is set for it.

<ScrollView
 xmlns:andro
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent">


 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">

 <TextView
 android:layout_width="match_parent"
 android:layout_height="300dp"
 android:text="Hello World Begin!"/>


 <EditText
 android:
 android:hint="EditText"
 android:layout_width="match_parent"
 android:layout_height="200dp"
 android:gravity="top"
 android:background="@drawable/rectangle_shape"/>

 <TextView
 android:layout_width="match_parent"
 android:layout_height="300dp"
 android:text="Hello World End!"/>
 </LinearLayout>

</ScrollView>

2.rectangle_shape

Background rectangle_shape's code has no technical content. . . . . .

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro>
 <solid android:color="#ffffff"/>
 <stroke android:color="#cccccc"
 android:width="1dp"/>

</shape>

Code in

This is the main code logic. First set the OnTouchListener for EditText, and then first determine whether the currently clicked area is EditText in the OnTouch method. If it is the EditText area, then determine whether it can scroll in the vertical direction. If it can scroll, the event will be handed over to EditText, otherwise the event will be handed over to ScrollView.
The most important thing here is how to judge that the EditText area can scroll in the vertical direction. The code here has been encapsulated into a method that everyone can use directly. So why do you need to make such a judgment? If you are still interested, please continue to read the perfect solution to the scroll conflict between EditText and ScrollView (Part 2).

public class MainActivity extends Activity implements  {

  private EditText mEditText;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    mEditText = (EditText) findViewById(.edit_text);
    (this);
  }

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
    //Touching EditText and the current EditText can be scrolled, then the event will be handed over to EditText for processing; otherwise, the event will be handed over to its parent class for processing    if ((() == .edit_text &amp;&amp; canVerticalScroll(mEditText))) {
      ().requestDisallowInterceptTouchEvent(true);
      if (() == MotionEvent.ACTION_UP) {
        ().requestDisallowInterceptTouchEvent(false);
      }
    }
    return false;
  }

  /**
    * Is EditText scrollable in vertical direction?
    * @param editText EditText that needs to be judged
    * @return true: Can be scrolled false: Can not be scrolled
    */
  private boolean canVerticalScroll(EditText editText) {
    //The scrolling distance    int scrollY = ();
    //Total height of control content    int scrollRange = ().getHeight();
    //The actual height of the control display    int scrollExtent = () - () -();
    //The difference between the total height of the control content and the actual display height    int scrollDifference = scrollRange - scrollExtent;

    if(scrollDifference == 0) {
      return false;
    }

    return (scrollY &gt; 0) || (scrollY &lt; scrollDifference - 1);
  }
}


The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.