The login
registered
Write an article
Home page
Download the APP

Android: ScrollView and nested EditText sliding conflict resolution

Carson_Ho

Android: ScrollView and nested EditText sliding conflict resolution



background

A ScrollView has an EditText (set to slide) embedded in it.

activity_main.xml

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/mEditText" android:layout_width="300dp" android:layout_height="300dp" android:maxLines="8" android:scrollbars="vertical" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:layout_width="match_parent" android:layout_height="500dp" android:text="Android" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> </ScrollView> </LinearLayout>Copy the code

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ScrollView mScrollView;
    EditText mEditText;

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

        mScrollView = findViewById(R.id.scrollView);
        mEditText = findViewById(R.id.mEditText);
}
Copy the code

The problem

MScrollView is slidable, but mEditText is not slidable.


Cause analysis,

The mScrollView consumes the event and does not pass it to the EditText, resulting in the EditText not being able to respond to the event


solution

  • When the touch isEditText& currentEditTextWhen scrollable, the event is givenEditTextProcess, that is, scroll
  • Otherwise, the event is assigned to its parent classScrollViewTo roll

Concrete implementation scheme

Autotype EditText the onTouch () and USES the requestDisallowInterceptTouchEvent (true) to intercept touch events

For more information on why you can intercept events, check out the event distribution mechanism I wrote: Android Event Distribution: Are you Worth it

public class MainActivity extends AppCompatActivity { ScrollView mScrollView; EditText mEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mScrollView = findViewById(R.id.scrollView); mEditText = findViewById(R.id.mEditText); . / / copy the onTouch method of EditText mEditText setOnTouchListener (new the OnTouchListener () {@ Override public Boolean the onTouch (View V, MotionEvent event) {// If the current EditText is scrollable, the event is passed to the EditText; if ((v.getId() == R.id.mEditText && canVerticalScroll(mEditText))) { v.getParent().requestDisallowInterceptTouchEvent(true); If (event.getAction() == motionEvent.action_up) {if (event.getAction() == motionEvent.action_up) { v.getParent().requestDisallowInterceptTouchEvent(false); } } return false; }}); } private Boolean canVerticalScroll(EditText EditText) {if (editText.getLinecount () > editText.getMaxLines()) { return true; } return false; }}Copy the code
  • This is a good way to resolve the ScrollView/nested EditText slide conflicts.

  • I’m going to continue to talk about Android development, but if you’re interested, please follow my Carson_Ho development notes

Thumb up, please! Because your encouragement is the biggest power that I write!

The Android event distribution mechanism is the most comprehensive and easy to understand solution for Android screen adaptation. It is the most comprehensive and easy to understand solution for Android screen adaptation. Android development: JSON introduction and the most comprehensive analysis method! BroadcastReceiver Is the most comprehensive version of Android’s BroadcastReceiver


Welcome to attentionCarson_HoJane books!

Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.