Last active
July 9, 2024 18:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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