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

+41-9
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

.idea/caches/build_file_checksums.ser

-603 Bytes
Binary file not shown.

.idea/codeStyles/Project.xml

-29
This file was deleted.

.idea/gradle.xml

-19
This file was deleted.

.idea/misc.xml

-33
This file was deleted.

.idea/runConfigurations.xml

-12
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

appbarlayoutbehavior/build.gradle

+2-5
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

+39-3
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

+20
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+

sample/build.gradle

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 27
4+
compileSdkVersion rootProject.ext.compileSdkVersion
55
defaultConfig {
66
applicationId "com.yuruiyin.sample"
7-
minSdkVersion 15
8-
targetSdkVersion 27
9-
versionCode 2
10-
versionName "1.0.1"
7+
minSdkVersion rootProject.ext.minSdkVersion
8+
buildToolsVersion rootProject.ext.buildToolsVersion
9+
targetSdkVersion rootProject.ext.targetSdkVersion
10+
versionCode 3
11+
versionName "1.0.2"
1112
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1213
}
1314
buildTypes {
@@ -25,9 +26,9 @@ android {
2526

2627
dependencies {
2728
implementation fileTree(dir: 'libs', include: ['*.jar'])
28-
implementation 'com.android.support:appcompat-v7:27.1.1'
29-
implementation 'com.android.support:design:27.1.1'
30-
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
29+
implementation "com.android.support:appcompat-v7:$androidSupportVersion"
30+
implementation "com.android.support:design:$androidSupportVersion"
31+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
3132
testImplementation 'junit:junit:4.12'
3233
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3334
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
@@ -39,6 +40,8 @@ dependencies {
3940

4041
implementation project(':appbarlayoutbehavior')
4142

42-
// implementation 'com.github.yuruiyin:AppbarLayoutBehavior:v1.0.0'
43+
// implementation 'com.github.yuruiyin:AppbarLayoutBehavior:v1.0.1'
4344

4445
}
46+
47+

sample/src/main/res/layout/activity_main.xml

+10-14
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
<android.support.design.widget.CollapsingToolbarLayout
2020
android:layout_width="match_parent"
2121
android:layout_height="wrap_content"
22-
app:layout_scrollFlags="scroll|exitUntilCollapsed">
22+
app:layout_scrollFlags="scroll|exitUntilCollapsed"
23+
>
2324

2425
<TextView
2526
android:layout_width="match_parent"
2627
android:layout_height="400dp"
2728
android:background="@color/green"
28-
app:layout_collapseMode="parallax"
2929
android:gravity="center"
30-
android:textSize="@dimen/common_text_size"
3130
android:text="这里是可折叠的内容"
31+
android:textSize="@dimen/common_text_size"
32+
app:layout_collapseMode="parallax"
3233
/>
3334

3435
</android.support.design.widget.CollapsingToolbarLayout>
@@ -40,7 +41,8 @@
4041
android:layout_height="@dimen/common_tab_height"
4142
android:layout_gravity="left"
4243
android:background="@color/white"
43-
android:visibility="visible"/>
44+
android:visibility="visible"
45+
/>
4446

4547
<View
4648
android:layout_width="match_parent"
@@ -50,18 +52,12 @@
5052

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

53-
<android.support.v4.widget.NestedScrollView
54-
android:id="@+id/nestedScrollview"
55+
<android.support.v4.view.ViewPager
56+
android:id="@+id/viewPager"
5557
android:layout_width="match_parent"
5658
android:layout_height="match_parent"
5759
android:fillViewport="true"
58-
app:layout_behavior="@string/appbar_scrolling_view_behavior">
59-
60-
<android.support.v4.view.ViewPager
61-
android:id="@+id/viewPager"
62-
android:layout_width="match_parent"
63-
android:layout_height="match_parent"/>
64-
65-
</android.support.v4.widget.NestedScrollView>
60+
app:layout_behavior="@string/appbar_scrolling_view_behavior"
61+
/>
6662

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

0 commit comments

Comments
 (0)