From 8bf190a6c1a0bf2a887a037fb00f978ba7c48106 Mon Sep 17 00:00:00 2001 From: woxingxiao Date: Sun, 28 Oct 2018 10:02:09 +0800 Subject: [PATCH] Fixed the unexpected view transitions of the ScrollView's directly child view which had been set margins; support api 28. --- app/build.gradle | 18 +++---- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 10 ++-- .../com/xw/repo/widget/BounceScrollView.java | 49 ++++++++++++++++--- 5 files changed, 59 insertions(+), 26 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f1c2b72..c0c326b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,17 +1,15 @@ apply plugin: 'com.android.application' - apply plugin: 'kotlin-android' - apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 27 + compileSdkVersion 28 defaultConfig { applicationId "com.xw.repo.bouncescrollviewsample" minSdkVersion 17 - targetSdkVersion 27 - versionCode 1 - versionName "1.0" + targetSdkVersion 28 + versionCode 2 + versionName "1.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { @@ -24,10 +22,10 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" - implementation 'com.android.support:appcompat-v7:27.1.1' - implementation "com.android.support:design:27.1.1" - implementation 'com.android.support.constraint:constraint-layout:1.1.1' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation "com.android.support:design:28.0.0" + implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' diff --git a/build.gradle b/build.gradle index e54ae2d..a3176b9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.30' + ext.kotlin_version = '1.2.71' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.3' + classpath 'com.android.tools.build:gradle:3.2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4f706c7..83a2511 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Jun 04 14:11:58 CST 2018 +#Sun Oct 28 09:09:51 CST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip diff --git a/library/build.gradle b/library/build.gradle index eaa5fbb..465e47a 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 27 + compileSdkVersion 28 defaultConfig { minSdkVersion 14 - targetSdkVersion 27 - versionCode 3 - versionName "1.2" + targetSdkVersion 28 + versionCode 4 + versionName "1.3" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -23,5 +23,5 @@ android { } dependencies { - implementation 'com.android.support:appcompat-v7:27.1.1' + compileOnly 'com.android.support:appcompat-v7:28.0.0' } diff --git a/library/src/main/java/com/xw/repo/widget/BounceScrollView.java b/library/src/main/java/com/xw/repo/widget/BounceScrollView.java index 127bca2..454b5db 100644 --- a/library/src/main/java/com/xw/repo/widget/BounceScrollView.java +++ b/library/src/main/java/com/xw/repo/widget/BounceScrollView.java @@ -76,6 +76,44 @@ protected void onFinishInflate() { } } + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + if (getChildCount() > 0) { + View child = getChildAt(0); + int childMeasuredHeight = child.getMeasuredHeight(); + if (childMeasuredHeight <= 0) + return; + int childMeasuredWidth = child.getMeasuredWidth(); + + int marginStart; + int topMargin; + int marginEnd; + int bottomMargin; + ViewGroup.LayoutParams lp = child.getLayoutParams(); + if (lp instanceof MarginLayoutParams) { + marginStart = MarginLayoutParamsCompat.getMarginStart((MarginLayoutParams) lp); + topMargin = ((MarginLayoutParams) lp).topMargin; + marginEnd = MarginLayoutParamsCompat.getMarginEnd((MarginLayoutParams) lp); + bottomMargin = ((MarginLayoutParams) lp).bottomMargin; + + if (marginStart != 0 || topMargin != 0 || marginEnd != 0 || bottomMargin != 0) { + if (childMeasuredHeight <= getMeasuredHeight()) { + childMeasuredWidth -= marginStart + marginEnd; + childMeasuredHeight -= topMargin + bottomMargin; + } else { + childMeasuredHeight += topMargin + bottomMargin; + } + int widthSpec = MeasureSpec.makeMeasureSpec(childMeasuredWidth, MeasureSpec.EXACTLY); + int heightSpec = MeasureSpec.makeMeasureSpec(childMeasuredHeight, MeasureSpec.EXACTLY); + + child.measure(widthSpec, heightSpec); + } + } + } + } + @Override public boolean canScrollVertically(int direction) { return !isHorizontal; @@ -117,10 +155,6 @@ public boolean onTouchEvent(MotionEvent ev) { return super.onTouchEvent(ev); switch (ev.getActionMasked()) { - case MotionEvent.ACTION_DOWN: - performClick(); - - break; case MotionEvent.ACTION_MOVE: float now, delta; int dampingDelta; @@ -160,6 +194,7 @@ public boolean onTouchEvent(MotionEvent ev) { break; case MotionEvent.ACTION_UP: + performClick(); case MotionEvent.ACTION_CANCEL: if (!mNormalRect.isEmpty()) { resetChildViewWithAnimation(); @@ -198,12 +233,12 @@ private void resetChildViewWithAnimation() { if (isHorizontal) { if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) { fixedPadding = ViewCompat.getPaddingEnd(this); - if (layoutParams != null && layoutParams instanceof MarginLayoutParams) { + if (layoutParams instanceof MarginLayoutParams) { fixedMargin = MarginLayoutParamsCompat.getMarginEnd((MarginLayoutParams) layoutParams); } } else { fixedPadding = ViewCompat.getPaddingStart(this); - if (layoutParams != null && layoutParams instanceof MarginLayoutParams) { + if (layoutParams instanceof MarginLayoutParams) { fixedMargin = MarginLayoutParamsCompat.getMarginStart((MarginLayoutParams) layoutParams); } } @@ -214,7 +249,7 @@ private void resetChildViewWithAnimation() { 0); } else { fixedPadding = getPaddingTop(); - if (layoutParams != null && layoutParams instanceof MarginLayoutParams) { + if (layoutParams instanceof MarginLayoutParams) { fixedMargin = ((MarginLayoutParams) layoutParams).topMargin; } anim = new TranslateAnimation(