Skip to content

Commit 207427b

Browse files
committed
解决support design 28版本不适配的问题
1 parent c083c4c commit 207427b

File tree

12 files changed

+124
-139
lines changed

12 files changed

+124
-139
lines changed

.gitignore

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1+
# Built application files
2+
*.apk
3+
*.ap_
4+
5+
# Files for the Dalvik VM
6+
*.dex
7+
8+
# Java class files
9+
*.class
10+
11+
# Generated files
12+
bin/
13+
gen/
14+
15+
# Gradle files
16+
.gradle/
17+
build/
18+
19+
# Local configuration file (sdk path, etc)
20+
local.properties
21+
22+
# Proguard folder generated by Eclipse
23+
proguard/
24+
25+
# Log Files
26+
*.log
27+
28+
# Android Studio Navigation editor temp files
29+
.navigation/
30+
31+
# Android Studio captures folder
32+
captures/
33+
34+
# Idea tool config
35+
.idea
136
*.iml
2-
.gradle
3-
/local.properties
4-
/.idea/libraries
5-
/.idea/modules.xml
6-
/.idea/workspace.xml
7-
.DS_Store
8-
/build
9-
/captures
10-
.externalNativeBuild
37+
38+
traces.txt
39+
40+
reports/
41+
42+
*.DS_Store
-603 Bytes
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.idea/gradle.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

.idea/runConfigurations.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

appbarlayoutbehavior/build.gradle

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ group='com.github.yuruiyin'
66
android {
77
compileSdkVersion 27
88

9-
10-
119
defaultConfig {
1210
minSdkVersion 15
1311
targetSdkVersion 27
12+
buildToolsVersion '27.0.3'
1413
versionCode 1
1514
versionName "1.0"
1615

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

3332
implementation 'com.android.support:appcompat-v7:27.1.1'
33+
implementation 'com.android.support:design:27.1.1'
3434
testImplementation 'junit:junit:4.12'
3535
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3636
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
37-
38-
implementation 'com.android.support:appcompat-v7:27.1.1'
39-
implementation 'com.android.support:design:27.1.1'
4037
}

appbarlayoutbehavior/src/main/java/com/yuruiyin/appbarlayoutbehavior/AppBarLayoutBehavior.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,52 @@ public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout chil
4949
return super.onInterceptTouchEvent(parent, child, ev);
5050
}
5151

52+
/**
53+
* 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
54+
* @return Field
55+
* @throws NoSuchFieldException
56+
*/
57+
private Field getFlingRunnableField() throws NoSuchFieldException {
58+
try {
59+
// support design 27及一下版本
60+
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
61+
return headerBehaviorType.getDeclaredField("mFlingRunnable");
62+
} catch (NoSuchFieldException e) {
63+
e.printStackTrace();
64+
// 可能是28及以上版本
65+
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
66+
return headerBehaviorType.getDeclaredField("flingRunnable");
67+
}
68+
}
69+
70+
/**
71+
* 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
72+
* @return Field
73+
* @throws NoSuchFieldException
74+
*/
75+
private Field getScrollerField() throws NoSuchFieldException {
76+
try {
77+
// support design 27及一下版本
78+
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
79+
return headerBehaviorType.getDeclaredField("mScroller");
80+
} catch (NoSuchFieldException e) {
81+
e.printStackTrace();
82+
// 可能是28及以上版本
83+
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
84+
return headerBehaviorType.getDeclaredField("scroller");
85+
}
86+
}
87+
5288
/**
5389
* 停止appbarLayout的fling事件
5490
* @param appBarLayout
5591
*/
5692
private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
5793
//通过反射拿到HeaderBehavior中的flingRunnable变量
5894
try {
59-
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
60-
Field flingRunnableField = headerBehaviorType.getDeclaredField("mFlingRunnable");
61-
Field scrollerField = headerBehaviorType.getDeclaredField("mScroller");
95+
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
96+
Field flingRunnableField = getFlingRunnableField();
97+
Field scrollerField = getScrollerField();
6298
flingRunnableField.setAccessible(true);
6399
scrollerField.setAccessible(true);
64100

build.gradle

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ allprojects {
2929
task clean(type: Delete) {
3030
delete rootProject.buildDir
3131
}
32+
33+
ext {
34+
compileSdkVersion = 28
35+
buildToolsVersion = '28.0.3'
36+
minSdkVersion = 15
37+
targetSdkVersion = 28
38+
androidSupportVersion = "28.0.0"
39+
}
40+
41+
subprojects {
42+
project.configurations.all {
43+
resolutionStrategy.eachDependency { details ->
44+
if (details.requested.group == 'com.android.support'
45+
&& !details.requested.name.contains('multidex') ) {
46+
details.useVersion androidSupportVersion
47+
}
48+
}
49+
}
50+
}
51+

0 commit comments

Comments
 (0)