Skip to content

Commit

Permalink
解决support design 28版本不适配的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yuruiyin committed Apr 3, 2019
1 parent c083c4c commit 207427b
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 139 deletions.
50 changes: 41 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# Idea tool config
.idea
*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild

traces.txt

reports/

*.DS_Store
Binary file removed .idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

This file was deleted.

19 changes: 0 additions & 19 deletions .idea/gradle.xml

This file was deleted.

33 changes: 0 additions & 33 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

7 changes: 2 additions & 5 deletions appbarlayoutbehavior/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ group='com.github.yuruiyin'
android {
compileSdkVersion 27



defaultConfig {
minSdkVersion 15
targetSdkVersion 27
buildToolsVersion '27.0.3'
versionCode 1
versionName "1.0"

Expand All @@ -31,10 +30,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
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'

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,52 @@ public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout chil
return super.onInterceptTouchEvent(parent, child, ev);
}

/**
* 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
* @return Field
* @throws NoSuchFieldException
*/
private Field getFlingRunnableField() throws NoSuchFieldException {
try {
// support design 27及一下版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("mFlingRunnable");
} catch (NoSuchFieldException e) {
e.printStackTrace();
// 可能是28及以上版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("flingRunnable");
}
}

/**
* 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
* @return Field
* @throws NoSuchFieldException
*/
private Field getScrollerField() throws NoSuchFieldException {
try {
// support design 27及一下版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("mScroller");
} catch (NoSuchFieldException e) {
e.printStackTrace();
// 可能是28及以上版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("scroller");
}
}

/**
* 停止appbarLayout的fling事件
* @param appBarLayout
*/
private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
//通过反射拿到HeaderBehavior中的flingRunnable变量
try {
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
Field flingRunnableField = headerBehaviorType.getDeclaredField("mFlingRunnable");
Field scrollerField = headerBehaviorType.getDeclaredField("mScroller");
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
Field flingRunnableField = getFlingRunnableField();
Field scrollerField = getScrollerField();
flingRunnableField.setAccessible(true);
scrollerField.setAccessible(true);

Expand Down
20 changes: 20 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ allprojects {
task clean(type: Delete) {
delete rootProject.buildDir
}

ext {
compileSdkVersion = 28
buildToolsVersion = '28.0.3'
minSdkVersion = 15
targetSdkVersion = 28
androidSupportVersion = "28.0.0"
}

subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion androidSupportVersion
}
}
}
}

21 changes: 12 additions & 9 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.yuruiyin.sample"
minSdkVersion 15
targetSdkVersion 27
versionCode 2
versionName "1.0.1"
minSdkVersion rootProject.ext.minSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 3
versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -25,9 +26,9 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
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.2'
implementation "com.android.support:appcompat-v7:$androidSupportVersion"
implementation "com.android.support:design:$androidSupportVersion"
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 All @@ -39,6 +40,8 @@ dependencies {

implementation project(':appbarlayoutbehavior')

// implementation 'com.github.yuruiyin:AppbarLayoutBehavior:v1.0.0'
// implementation 'com.github.yuruiyin:AppbarLayoutBehavior:v1.0.1'

}


24 changes: 10 additions & 14 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>

<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green"
app:layout_collapseMode="parallax"
android:gravity="center"
android:textSize="@dimen/common_text_size"
android:text="这里是可折叠的内容"
android:textSize="@dimen/common_text_size"
app:layout_collapseMode="parallax"
/>

</android.support.design.widget.CollapsingToolbarLayout>
Expand All @@ -40,7 +41,8 @@
android:layout_height="@dimen/common_tab_height"
android:layout_gravity="left"
android:background="@color/white"
android:visibility="visible"/>
android:visibility="visible"
/>

<View
android:layout_width="match_parent"
Expand All @@ -50,18 +52,12 @@

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollview"
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</android.support.v4.widget.NestedScrollView>
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

</android.support.design.widget.CoordinatorLayout>

0 comments on commit 207427b

Please sign in to comment.