feat(android): use NestedScrollView for vertical ScrollView (#9199)

This commit is contained in:
farfromrefuge
2022-11-24 06:46:32 +00:00
committed by GitHub
parent 046f9314a8
commit cfaa8134b0
4 changed files with 65 additions and 29 deletions

View File

@@ -8,14 +8,14 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import androidx.core.widget.NestedScrollView;
import org.nativescript.widgets.HorizontalScrollView.SavedState;
/**
* @author hhristov
*/
public class VerticalScrollView extends ScrollView {
public class VerticalScrollView extends NestedScrollView {
private final Rect mTempRect = new Rect();
@@ -33,9 +33,9 @@ public class VerticalScrollView extends ScrollView {
private boolean mIsLayoutDirty = true;
/**
* The child to give focus to in the event that a child has requested focus while the
* layout is dirty. This prevents the scroll from being wrong if the child has not been
* laid out before requesting focus.
* The child to give focus to in the event that a child has requested focus
* while the layout is dirty. This prevents the scroll from being wrong if the
* child has not been laid out before requesting focus.
*/
private View mChildToScrollTo = null;
@@ -67,7 +67,8 @@ public class VerticalScrollView extends ScrollView {
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!this.scrollEnabled && (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_MOVE)) {
if (!this.scrollEnabled
&& (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_MOVE)) {
return false;
}
@@ -131,7 +132,8 @@ public class VerticalScrollView extends ScrollView {
CommonLayoutParams.adjustChildrenLayoutParams(this, widthMeasureSpec, heightMeasureSpec);
// Don't call measure because it will measure content twice.
// ScrollView is expected to have single child so we measure only the first child.
// ScrollView is expected to have single child so we measure only the first
// child.
View child = this.getChildCount() > 0 ? this.getChildAt(0) : null;
if (child == null) {
this.scrollableLength = 0;
@@ -139,19 +141,23 @@ public class VerticalScrollView extends ScrollView {
this.contentMeasuredHeight = 0;
this.setPadding(0, 0, 0, 0);
} else {
CommonLayoutParams.measureChild(child, widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
CommonLayoutParams.measureChild(child, widthMeasureSpec,
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
this.contentMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
this.contentMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);
// Android ScrollView does not account to child margins so we set them as paddings. Otherwise you can never scroll to bottom.
// Android ScrollView does not account to child margins so we set them as
// paddings. Otherwise you can never scroll to bottom.
CommonLayoutParams lp = (CommonLayoutParams) child.getLayoutParams();
this.setPadding(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
}
// Don't add in our paddings because they are already added as child margins. (we will include them twice if we add them).
// check the previous line - this.setPadding(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
// this.contentMeasuredWidth += this.getPaddingLeft() + this.getPaddingRight();
// this.contentMeasuredHeight += this.getPaddingTop() + this.getPaddingBottom();
// Don't add in our paddings because they are already added as child margins.
// (we will include them twice if we add them).
// check the previous line - this.setPadding(lp.leftMargin, lp.topMargin,
// lp.rightMargin, lp.bottomMargin);
// this.contentMeasuredWidth += this.getPaddingLeft() + this.getPaddingRight();
// this.contentMeasuredHeight += this.getPaddingTop() + this.getPaddingBottom();
// Check against our minimum height
this.contentMeasuredWidth = Math.max(this.contentMeasuredWidth, this.getSuggestedMinimumWidth());
@@ -191,7 +197,8 @@ public class VerticalScrollView extends ScrollView {
if (this.isFirstLayout) {
this.isFirstLayout = false;
final int scrollRange = Math.max(0, childHeight - (bottom - top - this.getPaddingTop() - this.getPaddingBottom()));
final int scrollRange = Math.max(0,
childHeight - (bottom - top - this.getPaddingTop() - this.getPaddingBottom()));
if (this.mSavedState != null) {
scrollY = mSavedState.scrollPosition;
mSavedState = null;
@@ -212,13 +219,13 @@ public class VerticalScrollView extends ScrollView {
}
@Override
protected void onAttachedToWindow() {
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.isFirstLayout = true;
}
@Override
protected void onDetachedFromWindow() {
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
this.isFirstLayout = true;
}