Skip to content

Instantly share code, notes, and snippets.

@atoennis
Last active April 23, 2020 06:30
Show Gist options
  • Save atoennis/7549fba2634d0abccddf to your computer and use it in GitHub Desktop.
Save atoennis/7549fba2634d0abccddf to your computer and use it in GitHub Desktop.
Solution to having nested scrolling within Android. In this scenario a multiline EditText resides within a root level ScrollView. In order to have scroll momentum, the EditText itself doesn't scroll but it is wrapped within a ScrollView.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- More views can go here-->
<ScrollView
android:layout_width="320dp"
android:layout_height="284dp">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textCapSentences|textAutoCorrect|textMultiLine"
android:minHeight="284dp"/>
</ScrollView>
<!-- More views can go here-->
</ScrollView>
// When the EditText is touched, disable touches on it's ScrollView's parents.
// Once the user lifts up their finger, enable touches on on the ScrollView's parents once again.
@OnTouch(R.id.edit_text)
boolean handleNoteFieldTouch(View v, 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment