Skip to content

Commit

Permalink
Fixed the unexpected view transitions of the ScrollView's directly ch…
Browse files Browse the repository at this point in the history
…ild view which had been set margins; support api 28.
  • Loading branch information
woxingxiao committed Oct 28, 2018
1 parent 761d931 commit 8bf190a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
18 changes: 8 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -23,5 +23,5 @@ android {
}

dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
compileOnly 'com.android.support:appcompat-v7:28.0.0'
}
49 changes: 42 additions & 7 deletions library/src/main/java/com/xw/repo/widget/BounceScrollView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -160,6 +194,7 @@ public boolean onTouchEvent(MotionEvent ev) {

break;
case MotionEvent.ACTION_UP:
performClick();
case MotionEvent.ACTION_CANCEL:
if (!mNormalRect.isEmpty()) {
resetChildViewWithAnimation();
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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(
Expand Down

0 comments on commit 8bf190a

Please sign in to comment.