├── .DS_Store ├── .gitattributes ├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── README_CN.md ├── StackLayoutManager ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── littlemango │ │ └── stacklayoutmanager │ │ ├── DefaultAnimation.kt │ │ ├── DefaultLayout.kt │ │ ├── StackAnimation.kt │ │ ├── StackLayout.kt │ │ └── StackLayoutManager.kt │ └── res │ └── values │ └── strings.xml ├── build.gradle ├── gif ├── sample1.gif └── sample2.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample.png ├── sample ├── .DS_Store ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── .DS_Store │ ├── androidTest │ └── java │ │ └── com │ │ └── littlemango │ │ └── stacklayoutmanagermaster │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── .DS_Store │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── littlemango │ │ │ └── stacklayoutmanagermaster │ │ │ ├── FadeInFadeOutAnimation.java │ │ │ └── MainActivity.java │ └── res │ │ ├── .DS_Store │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ ├── ic_star_black_24dp.xml │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ ├── image3.jpg │ │ ├── image4.jpg │ │ ├── image5.jpg │ │ └── image6.jpg │ │ ├── layout │ │ ├── activity_main.xml │ │ └── image_card.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values-zh-rCN │ │ ├── arrays.xml │ │ └── strings.xml │ │ └── values │ │ ├── arrays.xml │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── littlemango │ └── stacklayoutmanagermaster │ └── ExampleUnitTest.kt └── settings.gradle /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/dictionaries 41 | .idea/libraries 42 | 43 | # Keystore files 44 | *.jks 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | 49 | # Google Services (e.g. APIs or Firebase) 50 | google-services.json 51 | 52 | # Freeline 53 | freeline.py 54 | freeline/ 55 | freeline_project_description.json 56 | .idea/vcs.xml 57 | .idea/caches/build_file_checksums.ser 58 | *.ser 59 | .DS_Store 60 | .DS_Store 61 | .idea/caches/build_file_checksums.ser 62 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Little Mango 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StackLayoutManager [中文说明](README_CN.md) 2 | -------- 3 | 4 | Android library that provides A RecyclerView.LayoutManager implementation which provides functionality to show a group of stack view. 5 | 6 | ![IMG](gif/sample1.gif) 7 | ![IMG](gif/sample2.gif) 8 | 9 | Overview 10 | -------- 11 | 12 | **StackLayoutManager** provides the following advantages: 13 | 14 | * **High performance**: the bottom layer use the recyclerView recycle mechanism to allow you to display a large number of itemviews without OOM. 15 | * **Flexibility**: you can choose which direction the card will slide out, and there are four directions to choose. 16 | * **Hight customization**: if you want to implement animation and layout yourself, you can inherit *StackAnimation* or *StackLayout* to achieve the desired effect. 17 | * **Easy to use**: you can start using it right away. All you need to do 18 | is to drop the JAR file into your project and replace *LinearLayoutManager* object in your code by *com.littlemango.stacklayoutmanager.StackLayoutManager* 19 | 20 | Gradle integration 21 | ------------------ 22 | 23 | If you're using Gradle, you can declare this library as a dependency: 24 | 25 | ```groovy 26 | dependencies { 27 | implementation 'com.littlemango:stacklayoutmanager:1.0.5' 28 | } 29 | ``` 30 | 31 | Basic usage 32 | ----------- 33 | 34 | The simplest way to use **StackLayoutManager** is by dropping the library JAR file into your project 35 | creating the *StackLayoutManager* object and pass it back to the RecyclerView object: 36 | 37 | ```java 38 | StackLayoutManager manager = new StackLayoutManager(); 39 | 40 | recyclerView.setLayoutManager(manager); 41 | ``` 42 | 43 | Advanced usage 44 | -------------- 45 | 46 | 1. You can set the orientation of the card to slide out of the screen: 47 | 48 | ```java 49 | //orientation can be one of ScrollOrientation.BOTTOM_TO_TOP or TOP_TO_BOTTOM or RIGHT_TO_LEFT or LEFT_TO_RIGHT 50 | ScrollOrientation orientation = ScrollOrientation.BOTTOM_TO_TOP 51 | 52 | StackLayoutManager manager = new StackLayoutManager(orientation); 53 | 54 | recyclerView.setLayoutManager(manager); 55 | ``` 56 | 57 | 2. You can set the visible item count: 58 | ```java 59 | //in the construction method 60 | StackLayoutManager manager = new StackLayoutManager(ScrollOrientation.BOTTOM_TO_TOP, 3); 61 | 62 | //or in setter method 63 | manager.setVisibleItemCount(3); 64 | ``` 65 | 66 | 3. You can change the offset between items: 67 | 68 | ```java 69 | manager.setItemOffset(50); 70 | ``` 71 | 72 | 4. You can scroll smoothly or immediately to the specified position: 73 | ```java 74 | //scroll to position with animation 75 | recyclerView.smoothScrollToPosition(3); 76 | 77 | //scroll to position immediately without animation 78 | recyclerView.scrollToPosition(3); 79 | ``` 80 | 5. You can set the view to turn only one page at a time, like a ViewPager, or you can turn pages continuously: 81 | ```java 82 | manager.setPagerMode(true or false); 83 | ``` 84 | 85 | 6. When in PagerView paging mode, you can set the minimum sliding velocity that triggers the paging effect: 86 | ```java 87 | manager.setPagerFlingVelocity(3000); 88 | ``` 89 | 90 | 7. You can add a item changed Listener to StackLayoutManager: 91 | ``` 92 | mStackLayoutManager.setItemChangedListener(new StackLayoutManager.ItemChangedListener() { 93 | @Override 94 | public void onItemChanged(int position) { 95 | mToast.setText("first visible item position is " + position); 96 | mToast.show(); 97 | } 98 | }); 99 | ``` 100 | 101 | 8. I use the DefaultAnimation class to provide animation, which is inherited from StackAnimation, and you can inherit both classes to achieve the desired animation effect: 102 | ```java 103 | DefaultAnimation animation = new DefaultAnimation(ScrollOrientation.BOTTOM_TO_TOP, visibleCount); 104 | manager.setAnimation(animation); 105 | ``` 106 | 9. I use the DefaultLayout class to implement the layout of items, which inherits from StackLayout. You can inherit both classes to achieve the layout effect you want: 107 | ```java 108 | StackLayoutManager manager = new StackLayoutManager(ScrollOrientation.BOTTOM_TO_TOP, 109 | visibleCount, 110 | DefaultAnimation.class, 111 | DefaultLayout.class); 112 | ``` 113 | 114 | Sample 115 | ------- 116 | You can clone or download this project to your computer and install **sample** apk on your phone to see the effect. 117 | 118 | Or you can scan QR code to install the apk: 119 | 120 | ![IMG](sample.png) 121 | 122 | License 123 | ------- 124 | MIT License 125 | See [MIT License](LICENSE) 126 | 127 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # StackLayoutManager 2 | -------- 3 | 4 | 一个实现了RecyclerView.LayoutManager的 Android 库,它能提供卡堆式展示效果。 5 | 6 | ![IMG](gif/sample1.gif) 7 | ![IMG](gif/sample2.gif) 8 | 9 | Overview 10 | -------- 11 | 12 | **StackLayoutManager** 有以下优点: 13 | 14 | * **高性能**: 底层利用缓存机制,循环利用滑出屏幕之外的视图,提高视图渲染效率,避免连续滑动后造成的OOM现象。 15 | * **灵活性**: 支持向上、向下、向左、向右四个滑动方向的配置。 16 | * **高自定义性**: 支持自定义动画、布局效果,只需要继承*StackAnimation*或*StackLayout*即可。 17 | * **容易使用**: 只需要创建*StackLayoutManager*对象,赋值给*RecyclerView*对象即可。 18 | 19 | Gradle integration 20 | ------------------ 21 | 22 | 如果你使用 Gradle,你可以在 gradle 文件中添加以下依赖: 23 | 24 | ```groovy 25 | dependencies { 26 | implementation 'com.littlemango:stacklayoutmanager:1.0.5' 27 | } 28 | ``` 29 | 30 | 基础使用 31 | ----------- 32 | 33 | 最简单的使用方法是直接调用*StackLayoutManager*的无参构造器实例化对象之后赋值给*RecyclerView*,默认的滑动方向是从右往左。 34 | 35 | ```java 36 | StackLayoutManager manager = new StackLayoutManager(); 37 | 38 | recyclerView.setLayoutManager(manager); 39 | ``` 40 | 41 | 高级使用 42 | -------------- 43 | 44 | 1. 你可以选择子控件滑出的方向: 45 | 46 | ```java 47 | //orientation can be one of ScrollOrientation.BOTTOM_TO_TOP or TOP_TO_BOTTOM or RIGHT_TO_LEFT or LEFT_TO_RIGHT 48 | ScrollOrientation orientation = ScrollOrientation.BOTTOM_TO_TOP 49 | 50 | StackLayoutManager manager = new StackLayoutManager(orientation); 51 | 52 | recyclerView.setLayoutManager(manager); 53 | ``` 54 | 55 | 2. 你可以设置在静止时可以看到的子视图的最大数量: 56 | ```java 57 | //在构造方法中指定可见数量 58 | StackLayoutManager manager = new StackLayoutManager(ScrollOrientation.BOTTOM_TO_TOP, 3); 59 | 60 | //或者在稍后的设置方法中修改 61 | manager.setVisibleItemCount(3); 62 | ``` 63 | 64 | 3. 你能修改子控件之间的间隔数值: 65 | 66 | ```java 67 | manager.setItemOffset(50); 68 | ``` 69 | 70 | 4. 你可以选择平滑还是立即滚动到指定位置,使用方法和其它 RecyclerView 一样,直接调用其相应方法: 71 | ```java 72 | //平滑的滚动到指定位置 73 | recyclerView.smoothScrollToPosition(3); 74 | 75 | //立即滚动到指定位置,没有动画效果 76 | recyclerView.scrollToPosition(3); 77 | ``` 78 | 5. 你可以设置像 ViewPager 一样的翻页模式,或者是普通的连续滚动模式: 79 | ```java 80 | manager.setPagerMode(true or false); 81 | ``` 82 | 83 | 6. 当处于 ViewPager 翻页模式下,你可以设置触发翻页效果的最小滑动速度: 84 | ```java 85 | manager.setPagerFlingVelocity(3000); 86 | ``` 87 | 88 | 7. 为 StackLayoutManager 增加一个 item 位置变化时的回调: 89 | ``` 90 | mStackLayoutManager.setItemChangedListener(new StackLayoutManager.ItemChangedListener() { 91 | @Override 92 | public void onItemChanged(int position) { 93 | mToast.setText("first visible item position is " + position); 94 | mToast.show(); 95 | } 96 | }); 97 | ``` 98 | 99 | 8. 如果你想自己实现动画效果,那么你可以继承自*StackAnimation*来定制动画效果,默认提供了[DefaultAnimation][DefaultAnimation]作为默认动画类: 100 | ```java 101 | DefaultAnimation animation = new DefaultAnimation(ScrollOrientation.BOTTOM_TO_TOP, visibleCount); 102 | manager.setAnimation(animation); 103 | ``` 104 | 9. 如果你想自己实现布局效果,那么你可以继承自*StackLayout*来定制布局效果,默认提供了[DefaultLayout][DefaultLayout]作为默认布局类: 105 | ```java 106 | StackLayoutManager manager = new StackLayoutManager(ScrollOrientation.BOTTOM_TO_TOP, 107 | visibleCount, 108 | DefaultAnimation.class, 109 | DefaultLayout.class); 110 | ``` 111 | 112 | Sample 113 | ------- 114 | 你可以克隆或者下载该工程到电脑上,运行安装应用到手机上即可查看例子. 115 | 116 | 或者,你可以扫码安装示例APK: 117 | 118 | ![IMG](sample.png) 119 | 120 | License 121 | ------- 122 | MIT License 123 | See [MIT License](LICENSE) 124 | 125 | [DefaultLayout]: StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/DefaultLayout.kt 126 | [DefaultAnimation]: StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/DefaultAnimation.kt 127 | -------------------------------------------------------------------------------- /StackLayoutManager/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /StackLayoutManager/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'com.novoda.bintray-release' 4 | 5 | android { 6 | compileSdkVersion 27 7 | 8 | defaultConfig { 9 | minSdkVersion 14 10 | targetSdkVersion 27 11 | versionCode 6 12 | versionName "1.0.5" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | sourceSets { 26 | main.java.srcDirs += 'src/main/kotlin' 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(include: ['*.jar'], dir: 'libs') 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | implementation 'com.android.support:recyclerview-v7:27.1.1' 34 | } 35 | repositories { 36 | mavenCentral() 37 | tasks.withType(Javadoc) { 38 | options{ encoding "UTF-8" 39 | charSet 'UTF-8' 40 | links "http://docs.oracle.com/javase/7/docs/api" 41 | } 42 | } 43 | } 44 | 45 | publish{ 46 | userOrg = 'jinliancheng120' 47 | groupId = 'com.littlemango' 48 | artifactId = 'stacklayoutmanager' 49 | publishVersion = '1.0.5' 50 | desc = 'A RecyclerView.LayoutManager implementation which provides functionality to show a group of stack view.' 51 | website = 'https://github.com/LittleMango/StackLayoutManager' 52 | licences = ['MIT'] 53 | } 54 | -------------------------------------------------------------------------------- /StackLayoutManager/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /StackLayoutManager/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/DefaultAnimation.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanager 2 | 3 | import android.view.View 4 | import com.littlemango.stacklayoutmanager.StackLayoutManager.ScrollOrientation 5 | 6 | class DefaultAnimation(scrollOrientation: ScrollOrientation, visibleCount: Int) : StackAnimation(scrollOrientation, visibleCount) { 7 | 8 | private var mScale = 0.95f 9 | private var mOutScale = 0.8f 10 | private var mOutRotation: Int 11 | 12 | init { 13 | mOutRotation = when(scrollOrientation) { 14 | ScrollOrientation.LEFT_TO_RIGHT, ScrollOrientation.RIGHT_TO_LEFT -> 10 15 | else -> 0 16 | } 17 | } 18 | 19 | /** 20 | * 设置 item 缩放比例. 21 | * @param scale 缩放比例,默认是0.95f. 22 | */ 23 | fun setItemScaleRate(scale: Float) { 24 | mScale = scale 25 | } 26 | 27 | /** 28 | * 获取item缩放比例. 29 | * @return item缩放比例,默认是0.95f. 30 | */ 31 | fun getItemScaleRate(): Float { 32 | return mScale 33 | } 34 | 35 | /** 36 | * 设置 itemView 离开屏幕时候的缩放比例. 37 | * @param scale 缩放比例,默认是0.8f. 38 | */ 39 | fun setOutScale(scale: Float) { 40 | mOutScale = scale 41 | } 42 | 43 | /** 44 | * 获取 itemView 离开屏幕时候的缩放比例. 45 | * @return 缩放比例,默认是0.8f. 46 | */ 47 | fun getOutScale(): Float { 48 | return mOutScale 49 | } 50 | 51 | /** 52 | * 设置 itemView 离开屏幕时候的旋转角度. 53 | * @param rotation 旋转角度,默认是30. 54 | */ 55 | fun setOutRotation(rotation: Int) { 56 | mOutRotation = rotation 57 | } 58 | 59 | /** 60 | * 获取 itemView 离开屏幕时候的旋转角度 61 | * @return 旋转角度,默认是30 62 | */ 63 | fun getOutRotation(): Int { 64 | return mOutRotation 65 | } 66 | 67 | override fun doAnimation(firstMovePercent: Float, itemView: View, position: Int) { 68 | val scale: Float 69 | var alpha = 1.0f 70 | val rotation: Float 71 | if (position == 0) { 72 | scale = 1 - ((1 - mOutScale) * firstMovePercent) 73 | rotation = mOutRotation * firstMovePercent 74 | } else { 75 | val minScale = (Math.pow(mScale.toDouble(), position.toDouble())).toFloat() 76 | val maxScale = (Math.pow(mScale.toDouble(), (position - 1).toDouble())).toFloat() 77 | scale = minScale + (maxScale - minScale) * firstMovePercent 78 | //只对最后一个 item 做透明度变化 79 | if (position == mVisibleCount) { 80 | alpha = firstMovePercent 81 | } 82 | rotation = 0f 83 | } 84 | 85 | setItemPivotXY(mScrollOrientation, itemView) 86 | rotationFirstVisibleItem(mScrollOrientation, itemView, rotation) 87 | itemView.scaleX = scale 88 | itemView.scaleY = scale 89 | itemView.alpha = alpha 90 | } 91 | 92 | private fun setItemPivotXY(scrollOrientation: ScrollOrientation, view: View) { 93 | when(scrollOrientation) { 94 | ScrollOrientation.RIGHT_TO_LEFT -> { 95 | view.pivotX = view.measuredWidth.toFloat() 96 | view.pivotY = view.measuredHeight.toFloat() / 2 97 | } 98 | ScrollOrientation.LEFT_TO_RIGHT -> { 99 | view.pivotX = 0f 100 | view.pivotY = view.measuredHeight.toFloat() / 2 101 | } 102 | ScrollOrientation.BOTTOM_TO_TOP -> { 103 | view.pivotX = view.measuredWidth.toFloat() /2 104 | view.pivotY = view.measuredHeight.toFloat() 105 | } 106 | ScrollOrientation.TOP_TO_BOTTOM -> { 107 | view.pivotX = view.measuredWidth.toFloat() / 2 108 | view.pivotY = 0f 109 | } 110 | } 111 | } 112 | 113 | private fun rotationFirstVisibleItem(scrollOrientation: ScrollOrientation, view: View, rotation: Float) { 114 | when(scrollOrientation) { 115 | ScrollOrientation.RIGHT_TO_LEFT -> view.rotationY = rotation 116 | ScrollOrientation.LEFT_TO_RIGHT -> view.rotationY = -rotation 117 | ScrollOrientation.BOTTOM_TO_TOP -> view.rotationX = -rotation 118 | ScrollOrientation.TOP_TO_BOTTOM -> view.rotationX = rotation 119 | } 120 | } 121 | } -------------------------------------------------------------------------------- /StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/DefaultLayout.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanager 2 | 3 | import android.view.View 4 | 5 | class DefaultLayout(scrollOrientation: StackLayoutManager.ScrollOrientation, 6 | visibleCount: Int, 7 | perItemOffset: Int) : StackLayout(scrollOrientation, visibleCount, perItemOffset) { 8 | 9 | private var mHasMeasureItemSize = false 10 | private var mWidthSpace = 0 11 | private var mHeightSpace = 0 12 | private var mStartMargin = 0 13 | 14 | private var mWidth = 0 15 | private var mHeight = 0 16 | private var mScrollOffset = 0 17 | 18 | override fun doLayout(stackLayoutManager: StackLayoutManager, scrollOffset: Int, firstMovePercent: Float, itemView: View, position: Int) { 19 | mWidth = stackLayoutManager.width 20 | mHeight = stackLayoutManager.height 21 | mScrollOffset = scrollOffset 22 | if (!mHasMeasureItemSize) { 23 | mWidthSpace = mWidth - stackLayoutManager.getDecoratedMeasuredWidth(itemView) 24 | mHeightSpace = mHeight - stackLayoutManager.getDecoratedMeasuredHeight(itemView) 25 | mStartMargin = getStartMargin() 26 | mHasMeasureItemSize = true 27 | } 28 | val left: Int 29 | val top: Int 30 | if (position == 0) { 31 | left = getFirstVisibleItemLeft() 32 | top = getFirstVisibleItemTop() 33 | } else { 34 | left = getAfterFirstVisibleItemLeft(position, firstMovePercent) 35 | top = getAfterFirstVisibleItemTop(position, firstMovePercent) 36 | } 37 | 38 | val right = left + stackLayoutManager.getDecoratedMeasuredWidth(itemView) 39 | val bottom = top + stackLayoutManager.getDecoratedMeasuredHeight(itemView) 40 | 41 | stackLayoutManager.layoutDecorated(itemView, left, top, right, bottom) 42 | } 43 | 44 | override fun requestLayout() { 45 | mHasMeasureItemSize = false //表示尺寸可能发生了改变 46 | } 47 | 48 | private fun getFirstVisibleItemLeft(): Int { 49 | return when(mScrollOrientation) { 50 | StackLayoutManager.ScrollOrientation.RIGHT_TO_LEFT -> mStartMargin - mScrollOffset % mWidth 51 | StackLayoutManager.ScrollOrientation.LEFT_TO_RIGHT -> { 52 | return if (mScrollOffset % mWidth == 0) { 53 | mStartMargin 54 | } else { 55 | mStartMargin + (mWidth - mScrollOffset % mWidth) 56 | } 57 | } 58 | else -> mWidthSpace / 2 59 | } 60 | } 61 | 62 | private fun getFirstVisibleItemTop(): Int { 63 | return when(mScrollOrientation) { 64 | StackLayoutManager.ScrollOrientation.BOTTOM_TO_TOP -> mStartMargin - mScrollOffset % mHeight 65 | StackLayoutManager.ScrollOrientation.TOP_TO_BOTTOM -> { 66 | return if (mScrollOffset % mHeight == 0) { 67 | mStartMargin 68 | } else { 69 | mStartMargin + (mHeight - mScrollOffset % mHeight) 70 | } 71 | } 72 | else -> mHeightSpace / 2 73 | } 74 | } 75 | 76 | private fun getAfterFirstVisibleItemLeft(visiblePosition: Int, movePercent: Float): Int { 77 | return when(mScrollOrientation) { 78 | StackLayoutManager.ScrollOrientation.RIGHT_TO_LEFT -> (mStartMargin + mPerItemOffset * (visiblePosition - movePercent)).toInt() 79 | StackLayoutManager.ScrollOrientation.LEFT_TO_RIGHT -> (mStartMargin - mPerItemOffset * (visiblePosition - movePercent)).toInt() 80 | else -> mWidthSpace / 2 81 | } 82 | } 83 | 84 | private fun getAfterFirstVisibleItemTop(visiblePosition: Int, movePercent: Float): Int { 85 | return when(mScrollOrientation) { 86 | StackLayoutManager.ScrollOrientation.BOTTOM_TO_TOP -> (mStartMargin + mPerItemOffset * (visiblePosition - movePercent)).toInt() 87 | StackLayoutManager.ScrollOrientation.TOP_TO_BOTTOM -> (mStartMargin - mPerItemOffset * (visiblePosition - movePercent)).toInt() 88 | else -> mHeightSpace / 2 89 | } 90 | } 91 | 92 | private fun getStartMargin(): Int { 93 | return when(mScrollOrientation) { 94 | StackLayoutManager.ScrollOrientation.RIGHT_TO_LEFT, StackLayoutManager.ScrollOrientation.LEFT_TO_RIGHT -> mWidthSpace / 2 95 | else -> mHeightSpace / 2 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackAnimation.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanager 2 | 3 | import android.view.View 4 | import com.littlemango.stacklayoutmanager.StackLayoutManager.ScrollOrientation 5 | 6 | abstract class StackAnimation(scrollOrientation: ScrollOrientation, visibleCount: Int) { 7 | 8 | protected val mScrollOrientation = scrollOrientation 9 | protected var mVisibleCount = visibleCount 10 | 11 | internal fun setVisibleCount(visibleCount: Int) { 12 | mVisibleCount = visibleCount 13 | } 14 | 15 | /** 16 | * 外部回调,用来做动画. 17 | * @param firstMovePercent 第一个可视 item 移动的百分比,当即将完全移出屏幕的时候 firstMovePercent无限接近1. 18 | * @param itemView 当前的 itemView. 19 | * @param position 当前 itemView 对应的位置,position = 0 until visibleCount. 20 | */ 21 | abstract fun doAnimation(firstMovePercent: Float, itemView: View, position: Int) 22 | } -------------------------------------------------------------------------------- /StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackLayout.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanager 2 | 3 | import android.view.View 4 | 5 | abstract class StackLayout(scrollOrientation: StackLayoutManager.ScrollOrientation, 6 | visibleCount: Int, 7 | perItemOffset: Int) { 8 | 9 | protected val mScrollOrientation = scrollOrientation 10 | protected var mVisibleCount = visibleCount 11 | protected var mPerItemOffset = perItemOffset 12 | 13 | internal fun setItemOffset(offset: Int) { 14 | mPerItemOffset = offset 15 | } 16 | 17 | internal fun getItemOffset(): Int { 18 | return mPerItemOffset 19 | } 20 | 21 | /** 22 | * 外部回调,用来做布局. 23 | * @param firstMovePercent 第一个可视 item 移动的百分比,当即将完全移出屏幕的时候 firstMovePercent无限接近1. 24 | * @param itemView 当前的 itemView. 25 | * @param position 当前 itemView 对应的位置,position = 0 until visibleCount. 26 | */ 27 | abstract fun doLayout(stackLayoutManager: StackLayoutManager, 28 | scrollOffset: Int, 29 | firstMovePercent: Float, 30 | itemView: View, 31 | position: Int) 32 | 33 | abstract fun requestLayout() 34 | } -------------------------------------------------------------------------------- /StackLayoutManager/src/main/java/com/littlemango/stacklayoutmanager/StackLayoutManager.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanager 2 | 3 | import android.support.annotation.IntRange 4 | import android.support.v7.widget.RecyclerView 5 | import android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING 6 | import android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE 7 | import android.view.View 8 | import android.view.ViewGroup 9 | 10 | class StackLayoutManager(scrollOrientation: ScrollOrientation, 11 | visibleCount: Int, 12 | animation: Class, 13 | layout: Class) : RecyclerView.LayoutManager() { 14 | private enum class FlingOrientation{NONE, LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP} 15 | 16 | enum class ScrollOrientation{LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP} 17 | 18 | private var mVisibleItemCount = visibleCount 19 | 20 | private var mScrollOrientation = scrollOrientation 21 | 22 | private var mScrollOffset: Int 23 | 24 | private lateinit var mOnScrollListener: RecyclerView.OnScrollListener 25 | private lateinit var mOnFlingListener: RecyclerView.OnFlingListener 26 | 27 | //做动画的组件,支持自定义 28 | private var mAnimation: StackAnimation? = null 29 | //做布局的组件,支持自定义 30 | private var mLayout: StackLayout? = null 31 | 32 | //是否是翻页效果 33 | private var mPagerMode = true 34 | 35 | //触发翻页效果的最低 Fling速度 36 | private var mPagerFlingVelocity = 0 37 | 38 | //标志当前滚动是否是调用scrollToCenter之后触发的滚动 39 | private var mFixScrolling = false 40 | 41 | //fling的方向,用来判断是前翻还是后翻 42 | private var mFlingOrientation = FlingOrientation.NONE 43 | 44 | //当前所处item对应的位置 45 | private var itemPosition = 0 46 | 47 | //判断item位置是否发生了改变 48 | private var isItemPositionChanged = false 49 | 50 | //item 位置发生改变的回调 51 | private var itemChangedListener: ItemChangedListener? = null 52 | 53 | interface ItemChangedListener { 54 | fun onItemChanged(position: Int) 55 | } 56 | 57 | /** 58 | * 设置是否为ViewPager 式翻页模式. 59 | *

60 | * 当设置为 true 的时候,可以配合[StackLayoutManager.setPagerFlingVelocity]设置触发翻页的最小速度. 61 | * @param isPagerMode 这个值默认是 false,当设置为 true 的时候,会有 viewPager 翻页效果. 62 | */ 63 | fun setPagerMode(isPagerMode: Boolean) { 64 | mPagerMode = isPagerMode 65 | } 66 | 67 | /** 68 | * @return 当前是否为ViewPager翻页模式. 69 | */ 70 | fun getPagerMode(): Boolean { 71 | return mPagerMode 72 | } 73 | 74 | /** 75 | * 设置触发ViewPager翻页效果的最小速度. 76 | *

77 | * 该值仅在 [StackLayoutManager.getPagerMode] == true的时候有效. 78 | * @param velocity 默认值是2000. 79 | */ 80 | fun setPagerFlingVelocity(@IntRange(from = 0, to = Int.MAX_VALUE.toLong()) velocity: Int) { 81 | mPagerFlingVelocity = Math.min(Int.MAX_VALUE, Math.max(0, velocity)) 82 | } 83 | 84 | /** 85 | * @return 当前触发翻页的最小 fling 速度. 86 | */ 87 | fun getPagerFlingVelocity(): Int { 88 | return mPagerFlingVelocity 89 | } 90 | 91 | /** 92 | * 设置recyclerView 静止时候可见的itemView 个数. 93 | * @param count 可见 itemView,默认为3 94 | */ 95 | fun setVisibleItemCount(@IntRange(from = 1, to = Long.MAX_VALUE)count: Int) { 96 | mVisibleItemCount = Math.min(itemCount - 1, Math.max(1, count)) 97 | mAnimation?.setVisibleCount(mVisibleItemCount) 98 | } 99 | 100 | /** 101 | * 获取recyclerView 静止时候可见的itemView 个数. 102 | * @return 静止时候可见的itemView 个数,默认为3. 103 | */ 104 | fun getVisibleItemCount(): Int { 105 | return mVisibleItemCount 106 | } 107 | 108 | /** 109 | * 设置 item 偏移值,即第 i 个 item 相对于 第 i-1个 item 在水平方向的偏移值,默认是40px. 110 | * @param offset 每个 item 相对于前一个的偏移值. 111 | */ 112 | fun setItemOffset(offset: Int) { 113 | mLayout?.setItemOffset(offset) 114 | } 115 | 116 | /** 117 | * 获取每个 item 相对于前一个的水平偏移值. 118 | * @return 每个 item 相对于前一个的水平偏移值. 119 | */ 120 | fun getItemOffset(): Int { 121 | return if (mLayout == null) { 122 | 0 123 | } else { 124 | mLayout!!.getItemOffset() 125 | } 126 | } 127 | 128 | /** 129 | * 设置item 移动动画. 130 | * @param animation item 移动动画. 131 | */ 132 | fun setAnimation(animation: StackAnimation) { 133 | mAnimation = animation 134 | } 135 | 136 | /** 137 | * 获取 item 移动动画. 138 | * @return item 移动动画. 139 | */ 140 | fun getAnimation(): StackAnimation? { 141 | return mAnimation 142 | } 143 | 144 | /** 145 | * 获取StackLayoutManager 的滚动方向. 146 | * @return StackLayoutManager 的滚动方向. 147 | */ 148 | fun getScrollOrientation(): ScrollOrientation { 149 | return mScrollOrientation 150 | } 151 | 152 | /** 153 | * 返回第一个可见 itemView 的位置. 154 | * @return 返回第一个可见 itemView 的位置. 155 | */ 156 | fun getFirstVisibleItemPosition(): Int { 157 | if (width == 0 || height == 0) { 158 | return 0 159 | } 160 | return when(mScrollOrientation) { 161 | ScrollOrientation.RIGHT_TO_LEFT -> Math.floor((mScrollOffset * 1.0 / width)).toInt() 162 | ScrollOrientation.LEFT_TO_RIGHT -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / width)).toInt() 163 | ScrollOrientation.BOTTOM_TO_TOP -> Math.floor((mScrollOffset * 1.0 / height)).toInt() 164 | ScrollOrientation.TOP_TO_BOTTOM -> itemCount - 1 - Math.ceil((mScrollOffset * 1.0 / height)).toInt() 165 | } 166 | } 167 | 168 | /** 169 | * 设置 item 位置改变时触发的回调 170 | */ 171 | fun setItemChangedListener(listener: ItemChangedListener) { 172 | itemChangedListener = listener 173 | } 174 | 175 | constructor(scrollOrientation: ScrollOrientation) : this(scrollOrientation, 3, DefaultAnimation::class.java, DefaultLayout::class.java) 176 | 177 | constructor(scrollOrientation: ScrollOrientation, visibleCount: Int) : this(scrollOrientation, visibleCount, DefaultAnimation::class.java, DefaultLayout::class.java) 178 | 179 | constructor() : this(ScrollOrientation.RIGHT_TO_LEFT) 180 | 181 | init { 182 | mScrollOffset = when(mScrollOrientation) { 183 | ScrollOrientation.RIGHT_TO_LEFT, ScrollOrientation.BOTTOM_TO_TOP -> 0 184 | else -> Int.MAX_VALUE 185 | } 186 | 187 | if (StackAnimation::class.java.isAssignableFrom(animation)) { 188 | try { 189 | val cla = animation.getDeclaredConstructor(ScrollOrientation::class.java, Int::class.javaPrimitiveType) 190 | mAnimation = cla.newInstance(scrollOrientation, visibleCount) as StackAnimation 191 | } catch (e: Exception) { 192 | e.printStackTrace() 193 | } 194 | } 195 | if (StackLayout::class.java.isAssignableFrom(layout)) { 196 | try { 197 | val cla = layout.getDeclaredConstructor(ScrollOrientation::class.java, Int::class.javaPrimitiveType, Int::class.javaPrimitiveType) 198 | mLayout = cla.newInstance(scrollOrientation, visibleCount, 30) as StackLayout 199 | } catch (e: Exception) { 200 | e.printStackTrace() 201 | } 202 | } 203 | } 204 | 205 | override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams { 206 | return RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 207 | ViewGroup.LayoutParams.WRAP_CONTENT) 208 | } 209 | 210 | override fun onAttachedToWindow(view: RecyclerView) { 211 | super.onAttachedToWindow(view) 212 | mOnFlingListener = object : RecyclerView.OnFlingListener() { 213 | override fun onFling(velocityX: Int, velocityY: Int): Boolean { 214 | if (mPagerMode) { 215 | when(mScrollOrientation) { 216 | ScrollOrientation.RIGHT_TO_LEFT, ScrollOrientation.LEFT_TO_RIGHT -> { 217 | mFlingOrientation = when { 218 | velocityX > mPagerFlingVelocity -> FlingOrientation.RIGHT_TO_LEFT 219 | velocityX < -mPagerFlingVelocity -> FlingOrientation.LEFT_TO_RIGHT 220 | else -> FlingOrientation.NONE 221 | } 222 | if (mScrollOffset in 1 until width * (itemCount - 1)) { //边界不需要滚动 223 | mFixScrolling = true 224 | } 225 | } 226 | else -> { 227 | mFlingOrientation = when { 228 | velocityY > mPagerFlingVelocity -> FlingOrientation.BOTTOM_TO_TOP 229 | velocityY < -mPagerFlingVelocity -> FlingOrientation.TOP_TO_BOTTOM 230 | else -> FlingOrientation.NONE 231 | } 232 | if (mScrollOffset in 1 until width * (itemCount - 1)) { //边界不需要滚动 233 | mFixScrolling = true 234 | } 235 | } 236 | } 237 | calculateAndScrollToTarget(view) 238 | } 239 | return mPagerMode 240 | } 241 | } 242 | view.onFlingListener = mOnFlingListener 243 | 244 | mOnScrollListener = object : RecyclerView.OnScrollListener() { 245 | override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { 246 | if (newState == SCROLL_STATE_IDLE) { 247 | if (!mFixScrolling) { 248 | mFixScrolling = true 249 | calculateAndScrollToTarget(view) 250 | } else { 251 | //表示此次 IDLE 是由修正位置结束触发的 252 | mFixScrolling = false 253 | } 254 | } else if (newState == SCROLL_STATE_DRAGGING) { 255 | mFixScrolling = false 256 | } 257 | } 258 | } 259 | view.addOnScrollListener(mOnScrollListener) 260 | } 261 | 262 | override fun onDetachedFromWindow(view: RecyclerView?, recycler: RecyclerView.Recycler?) { 263 | super.onDetachedFromWindow(view, recycler) 264 | if (view?.onFlingListener == mOnFlingListener) { 265 | view.onFlingListener = null 266 | } 267 | view?.removeOnScrollListener(mOnScrollListener) 268 | } 269 | 270 | override fun canScrollHorizontally(): Boolean { 271 | if (itemCount == 0) { 272 | return false 273 | } 274 | return when (mScrollOrientation) { 275 | ScrollOrientation.LEFT_TO_RIGHT, ScrollOrientation.RIGHT_TO_LEFT -> true 276 | else -> false 277 | } 278 | } 279 | 280 | override fun canScrollVertically(): Boolean { 281 | if (itemCount == 0) { 282 | return false 283 | } 284 | return when (mScrollOrientation) { 285 | ScrollOrientation.TOP_TO_BOTTOM, ScrollOrientation.BOTTOM_TO_TOP -> true 286 | else -> false 287 | } 288 | } 289 | 290 | override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) { 291 | 292 | mLayout?.requestLayout() 293 | 294 | removeAndRecycleAllViews(recycler) 295 | 296 | if (itemCount > 0) { 297 | mScrollOffset = getValidOffset(mScrollOffset) 298 | loadItemView(recycler) 299 | } 300 | } 301 | 302 | override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int { 303 | return handleScrollBy(dx, recycler) 304 | } 305 | 306 | override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State?): Int { 307 | return handleScrollBy(dy, recycler) 308 | } 309 | 310 | override fun scrollToPosition(position: Int) { 311 | if (position < 0 || position >= itemCount) { 312 | throw ArrayIndexOutOfBoundsException("$position is out of bound [0..$itemCount-1]") 313 | } 314 | mScrollOffset = getPositionOffset(position) 315 | requestLayout() 316 | } 317 | 318 | override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, position: Int) { 319 | if (position < 0 || position >= itemCount) { 320 | throw ArrayIndexOutOfBoundsException("$position is out of bound [0..$itemCount-1]") 321 | } 322 | mFixScrolling = true 323 | scrollToCenter(position, recyclerView, true) 324 | } 325 | 326 | private fun updatePositionRecordAndNotify(position: Int) { 327 | if (itemChangedListener == null) { 328 | return 329 | } 330 | if (position != itemPosition) { 331 | isItemPositionChanged = true 332 | itemPosition = position 333 | itemChangedListener?.onItemChanged(itemPosition) 334 | } else { 335 | isItemPositionChanged = false 336 | } 337 | } 338 | 339 | private fun handleScrollBy(offset: Int, recycler: RecyclerView.Recycler): Int { 340 | //期望值,不得超过最大最小值,所以期望值不一定等于实际值 341 | val expectOffset = mScrollOffset + offset 342 | 343 | //实际值 344 | mScrollOffset = getValidOffset(expectOffset) 345 | 346 | //实际偏移,超过最大最小值之后的偏移都应该是0,该值作为返回值,否则在极限位置进行滚动的时候不会出现弹性阴影 347 | val exactMove = mScrollOffset - expectOffset + offset 348 | 349 | if (exactMove == 0) { 350 | //itemViews 位置都不会改变,直接 return 351 | return 0 352 | } 353 | 354 | detachAndScrapAttachedViews(recycler) 355 | 356 | loadItemView(recycler) 357 | return exactMove 358 | } 359 | 360 | private fun loadItemView(recycler: RecyclerView.Recycler) { 361 | val firstVisiblePosition = getFirstVisibleItemPosition() 362 | val lastVisiblePosition = getLastVisibleItemPosition() 363 | 364 | //位移百分比 365 | val movePercent = getFirstVisibleItemMovePercent() 366 | 367 | for (i in lastVisiblePosition downTo firstVisiblePosition) { 368 | val view = recycler.getViewForPosition(i) 369 | //添加到recycleView 中 370 | addView(view) 371 | //测量 372 | measureChild(view, 0, 0) 373 | //布局 374 | mLayout?.doLayout(this, mScrollOffset, movePercent, view, i - firstVisiblePosition) 375 | //做动画 376 | mAnimation?.doAnimation(movePercent, view, i - firstVisiblePosition) 377 | } 378 | 379 | //尝试更新当前item的位置并通知外界 380 | updatePositionRecordAndNotify(firstVisiblePosition) 381 | 382 | //重用 383 | if (firstVisiblePosition - 1 >= 0) { 384 | val view = recycler.getViewForPosition(firstVisiblePosition - 1) 385 | resetViewAnimateProperty(view) 386 | removeAndRecycleView(view, recycler) 387 | } 388 | if (lastVisiblePosition + 1 < itemCount) { 389 | val view = recycler.getViewForPosition(lastVisiblePosition + 1) 390 | resetViewAnimateProperty(view) 391 | removeAndRecycleView(view, recycler) 392 | } 393 | } 394 | 395 | private fun resetViewAnimateProperty(view: View) { 396 | view.rotationY = 0f 397 | view.rotationX = 0f 398 | view.scaleX = 1f 399 | view.scaleY = 1f 400 | view.alpha = 1f 401 | } 402 | 403 | private fun calculateAndScrollToTarget(view: RecyclerView) { 404 | val targetPosition = calculateCenterPosition(getFirstVisibleItemPosition()) 405 | scrollToCenter(targetPosition, view, true) 406 | } 407 | 408 | private fun scrollToCenter(targetPosition: Int, recyclerView: RecyclerView, animation: Boolean) { 409 | val targetOffset = getPositionOffset(targetPosition) 410 | when(mScrollOrientation) { 411 | ScrollOrientation.LEFT_TO_RIGHT, ScrollOrientation.RIGHT_TO_LEFT -> { 412 | if (animation) { 413 | recyclerView.smoothScrollBy(targetOffset - mScrollOffset, 0) 414 | } else { 415 | recyclerView.scrollBy(targetOffset - mScrollOffset, 0) 416 | } 417 | } 418 | else -> { 419 | if (animation) { 420 | recyclerView.smoothScrollBy(0, targetOffset - mScrollOffset) 421 | } else { 422 | recyclerView.scrollBy(0, targetOffset - mScrollOffset) 423 | } 424 | } 425 | } 426 | } 427 | 428 | private fun getValidOffset(expectOffset: Int): Int { 429 | return when(mScrollOrientation) { 430 | ScrollOrientation.RIGHT_TO_LEFT, ScrollOrientation.LEFT_TO_RIGHT -> Math.max(Math.min(width * (itemCount - 1), expectOffset), 0) 431 | else -> Math.max(Math.min(height * (itemCount - 1), expectOffset), 0) 432 | } 433 | } 434 | 435 | private fun getPositionOffset(position: Int): Int { 436 | return when(mScrollOrientation) { 437 | ScrollOrientation.RIGHT_TO_LEFT -> position * width 438 | ScrollOrientation.LEFT_TO_RIGHT -> (itemCount - 1 - position) * width 439 | ScrollOrientation.BOTTOM_TO_TOP -> position * height 440 | ScrollOrientation.TOP_TO_BOTTOM -> (itemCount - 1 - position) * height 441 | } 442 | } 443 | 444 | private fun getLastVisibleItemPosition(): Int { 445 | val firstVisiblePosition = getFirstVisibleItemPosition() 446 | return if (firstVisiblePosition + mVisibleItemCount > itemCount - 1) { 447 | itemCount - 1 448 | } else { 449 | firstVisiblePosition + mVisibleItemCount 450 | } 451 | } 452 | 453 | private fun getFirstVisibleItemMovePercent(): Float { 454 | if (width == 0 || height == 0) { 455 | return 0f 456 | } 457 | return when (mScrollOrientation) { 458 | ScrollOrientation.RIGHT_TO_LEFT -> (mScrollOffset % width) * 1.0f / width 459 | ScrollOrientation.LEFT_TO_RIGHT -> { 460 | val targetPercent = 1 - (mScrollOffset % width) * 1.0f / width 461 | return if (targetPercent == 1f) { 462 | 0f 463 | } else { 464 | targetPercent 465 | } 466 | } 467 | ScrollOrientation.BOTTOM_TO_TOP -> (mScrollOffset % height) * 1.0f / height 468 | ScrollOrientation.TOP_TO_BOTTOM -> { 469 | val targetPercent = 1 - (mScrollOffset % height) * 1.0f / height 470 | return if (targetPercent == 1f) { 471 | 0f 472 | } else { 473 | targetPercent 474 | } 475 | } 476 | } 477 | } 478 | 479 | private fun calculateCenterPosition(position: Int): Int { 480 | //当是 Fling 触发的时候 481 | val triggerOrientation = mFlingOrientation 482 | mFlingOrientation = FlingOrientation.NONE 483 | when(mScrollOrientation) { 484 | ScrollOrientation.RIGHT_TO_LEFT -> { 485 | if (triggerOrientation == FlingOrientation.RIGHT_TO_LEFT) { 486 | return position + 1 487 | } else if (triggerOrientation == FlingOrientation.LEFT_TO_RIGHT) { 488 | return position 489 | } 490 | } 491 | ScrollOrientation.LEFT_TO_RIGHT -> { 492 | if (triggerOrientation == FlingOrientation.LEFT_TO_RIGHT) { 493 | return position + 1 494 | } else if (triggerOrientation == FlingOrientation.RIGHT_TO_LEFT) { 495 | return position 496 | } 497 | } 498 | ScrollOrientation.BOTTOM_TO_TOP -> { 499 | if (triggerOrientation == FlingOrientation.BOTTOM_TO_TOP) { 500 | return position + 1 501 | } else if (triggerOrientation == FlingOrientation.TOP_TO_BOTTOM) { 502 | return position 503 | } 504 | } 505 | ScrollOrientation.TOP_TO_BOTTOM -> { 506 | if (triggerOrientation == FlingOrientation.TOP_TO_BOTTOM) { 507 | return position + 1 508 | } else if (triggerOrientation == FlingOrientation.BOTTOM_TO_TOP) { 509 | return position 510 | } 511 | } 512 | } 513 | 514 | //当不是 fling 触发的时候 515 | val percent = getFirstVisibleItemMovePercent() 516 | //向左移动超过50% position(firstVisibleItemPosition)++ 517 | //否 position不变 518 | return if (percent < 0.5) { 519 | position 520 | } else { 521 | position + 1 522 | } 523 | } 524 | } -------------------------------------------------------------------------------- /StackLayoutManager/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | StackLayoutManager 3 | 4 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.2.60' 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.4' 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | classpath 'com.novoda:bintray-release:0.8.1' 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | google() 19 | jcenter() 20 | } 21 | tasks.withType(Javadoc) { 22 | options{ encoding "UTF-8" 23 | charSet 'UTF-8' 24 | links "http://docs.oracle.com/javase/7/docs/api" 25 | } 26 | } 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | tasks.getByPath(":StackLayoutManager:releaseAndroidJavadocs").enabled = false 33 | -------------------------------------------------------------------------------- /gif/sample1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/gif/sample1.gif -------------------------------------------------------------------------------- /gif/sample2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/gif/sample2.gif -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jun 28 17:28:50 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample.png -------------------------------------------------------------------------------- /sample/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/.DS_Store -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 27 9 | defaultConfig { 10 | applicationId "com.littlemango.stacklayoutmanagermaster" 11 | minSdkVersion 19 12 | targetSdkVersion 27 13 | versionCode 6 14 | versionName "1.0.5" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | ext.kotlin_version = '1.2.60' 26 | 27 | dependencies { 28 | implementation fileTree(include: ['*.jar'], dir: 'libs') 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | implementation 'com.android.support:appcompat-v7:27.1.1' 31 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 34 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 35 | implementation 'com.android.support:recyclerview-v7:27.1.1' 36 | implementation 'com.android.support:cardview-v7:27.1.1' 37 | implementation project(':StackLayoutManager') 38 | implementation 'com.afollestad.material-dialogs:core:0.9.6.0' 39 | implementation 'com.android.support:design:27.1.1' 40 | } 41 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/.DS_Store -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/littlemango/stacklayoutmanagermaster/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanagermaster 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.littlemango.stacklayoutmanagermaster", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/src/main/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/.DS_Store -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/java/com/littlemango/stacklayoutmanagermaster/FadeInFadeOutAnimation.java: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanagermaster; 2 | 3 | import android.support.v7.widget.LinearLayoutManager; 4 | import android.view.View; 5 | 6 | import com.littlemango.stacklayoutmanager.StackAnimation; 7 | import com.littlemango.stacklayoutmanager.StackLayoutManager.ScrollOrientation; 8 | 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | public class FadeInFadeOutAnimation extends StackAnimation { 12 | 13 | private int mVisibleCount; 14 | 15 | FadeInFadeOutAnimation(@NotNull ScrollOrientation scrollOrientation, int visibleCount) { 16 | super(scrollOrientation, visibleCount); 17 | mVisibleCount = visibleCount; 18 | } 19 | 20 | @Override 21 | public void doAnimation(float firstMovePercent, @NotNull View itemView, int position) { 22 | if (position == 0) { 23 | itemView.setAlpha(1 - firstMovePercent + 0.5f); 24 | } else if (position == mVisibleCount) { 25 | itemView.setAlpha(firstMovePercent); 26 | } 27 | itemView.setScaleX(1); 28 | itemView.setScaleY(1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /sample/src/main/java/com/littlemango/stacklayoutmanagermaster/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanagermaster; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.util.Log; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.ImageView; 13 | import android.widget.TextView; 14 | import android.widget.Toast; 15 | 16 | import com.afollestad.materialdialogs.MaterialDialog; 17 | import com.littlemango.stacklayoutmanager.StackLayoutManager; 18 | import com.littlemango.stacklayoutmanager.StackLayoutManager.ScrollOrientation; 19 | import java.util.Random; 20 | 21 | public class MainActivity extends AppCompatActivity { 22 | 23 | private static final String TAG = "MainActivity"; 24 | private RecyclerView mRecyclerView; 25 | 26 | private static final int mStackCount = 30; 27 | 28 | private int mRandomPosition; 29 | 30 | private StackLayoutManager mStackLayoutManager; 31 | 32 | private String[] selectItems; 33 | 34 | private Toast mToast; 35 | 36 | @Override 37 | protected void onCreate(@Nullable Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setContentView(R.layout.activity_main); 40 | mToast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT); 41 | mRecyclerView = findViewById(R.id.recycleView); 42 | 43 | mStackLayoutManager = new StackLayoutManager(); 44 | mRecyclerView.setLayoutManager(mStackLayoutManager); 45 | mRecyclerView.setAdapter(new StackLayoutAdapter()); 46 | 47 | selectItems = getResources().getStringArray(R.array.items); 48 | resetRandom(); 49 | 50 | findViewById(R.id.floatButton).setOnClickListener(new View.OnClickListener() { 51 | @Override 52 | public void onClick(View view) { 53 | 54 | new MaterialDialog.Builder(MainActivity.this) 55 | .items(selectItems) 56 | .itemsCallback(new MaterialDialog.ListCallback() { 57 | @Override 58 | public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { 59 | switch (which) { 60 | case 0: 61 | mRecyclerView.smoothScrollToPosition(mRandomPosition); 62 | resetRandom(); 63 | break; 64 | case 1: 65 | mRecyclerView.scrollToPosition(mRandomPosition); 66 | resetRandom(); 67 | break; 68 | case 2: 69 | mStackLayoutManager = new StackLayoutManager(ScrollOrientation.LEFT_TO_RIGHT); 70 | mRecyclerView.setLayoutManager(mStackLayoutManager); 71 | getSupportActionBar().setTitle("Picture 0"); 72 | break; 73 | case 3: 74 | mStackLayoutManager = new StackLayoutManager(ScrollOrientation.RIGHT_TO_LEFT); 75 | mRecyclerView.setLayoutManager(mStackLayoutManager); 76 | getSupportActionBar().setTitle("Picture 0"); 77 | break; 78 | case 4: 79 | mStackLayoutManager = new StackLayoutManager(ScrollOrientation.TOP_TO_BOTTOM); 80 | mRecyclerView.setLayoutManager(mStackLayoutManager); 81 | getSupportActionBar().setTitle("Picture 0"); 82 | break; 83 | case 5: 84 | mStackLayoutManager = new StackLayoutManager(ScrollOrientation.BOTTOM_TO_TOP); 85 | mRecyclerView.setLayoutManager(mStackLayoutManager); 86 | getSupportActionBar().setTitle("Picture 0"); 87 | break; 88 | case 6: 89 | mStackLayoutManager.setPagerMode(!mStackLayoutManager.getPagerMode()); 90 | break; 91 | case 7: 92 | mStackLayoutManager.setItemOffset(mStackLayoutManager.getItemOffset() + 10); 93 | mStackLayoutManager.requestLayout(); 94 | break; 95 | case 8: 96 | mStackLayoutManager.setItemOffset(mStackLayoutManager.getItemOffset() - 10); 97 | mStackLayoutManager.requestLayout(); 98 | break; 99 | case 9: 100 | mStackLayoutManager.setVisibleItemCount(mStackLayoutManager.getVisibleItemCount() + 1); 101 | mStackLayoutManager.requestLayout(); 102 | break; 103 | case 10: 104 | mStackLayoutManager.setVisibleItemCount(mStackLayoutManager.getVisibleItemCount() - 1); 105 | mStackLayoutManager.requestLayout(); 106 | break; 107 | case 11: 108 | mStackLayoutManager.setPagerFlingVelocity(mStackLayoutManager.getPagerFlingVelocity() + 5000); 109 | break; 110 | case 12: 111 | mStackLayoutManager.setPagerFlingVelocity(mStackLayoutManager.getPagerFlingVelocity() - 5000); 112 | break; 113 | case 13: 114 | mStackLayoutManager.setAnimation(new FadeInFadeOutAnimation(mStackLayoutManager.getScrollOrientation(), 115 | mStackLayoutManager.getVisibleItemCount())); 116 | mStackLayoutManager.requestLayout(); 117 | break; 118 | } 119 | 120 | mStackLayoutManager.setItemChangedListener(new StackLayoutManager.ItemChangedListener() { 121 | @Override 122 | public void onItemChanged(int position) { 123 | getSupportActionBar().setTitle("Picture " + position); 124 | } 125 | }); 126 | } 127 | }) 128 | .show(); 129 | } 130 | }); 131 | 132 | mStackLayoutManager.setItemChangedListener(new StackLayoutManager.ItemChangedListener() { 133 | @Override 134 | public void onItemChanged(int position) { 135 | getSupportActionBar().setTitle("Picture " + position); 136 | } 137 | }); 138 | } 139 | 140 | class StackLayoutAdapter extends RecyclerView.Adapter { 141 | 142 | @NonNull 143 | @Override 144 | public StackHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { 145 | 146 | View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_card, viewGroup, false); 147 | return new StackHolder(view); 148 | } 149 | 150 | @Override 151 | public void onBindViewHolder(@NonNull final StackHolder stackHolder, int position) { 152 | int res; 153 | switch (position % 6) { 154 | case 0: 155 | res = R.drawable.image1; 156 | break; 157 | case 1: 158 | res = R.drawable.image2; 159 | break; 160 | case 2: 161 | res = R.drawable.image3; 162 | break; 163 | case 3: 164 | res = R.drawable.image4; 165 | break; 166 | case 4: 167 | res = R.drawable.image5; 168 | break; 169 | default: 170 | res = R.drawable.image6; 171 | break; 172 | } 173 | stackHolder.imageView.setImageResource(res); 174 | stackHolder.textView.setText("" + position); 175 | stackHolder.itemView.setOnClickListener(new View.OnClickListener() { 176 | @Override 177 | public void onClick(View v) { 178 | if (stackHolder.getAdapterPosition() == mStackLayoutManager.getFirstVisibleItemPosition()) { 179 | mToast.setText("position: " + stackHolder.getAdapterPosition() + " is click!"); 180 | mToast.show(); 181 | } else { 182 | mRecyclerView.smoothScrollToPosition(stackHolder.getAdapterPosition()); 183 | } 184 | } 185 | }); 186 | } 187 | 188 | @Override 189 | public int getItemCount() { 190 | return mStackCount; 191 | } 192 | 193 | class StackHolder extends RecyclerView.ViewHolder { 194 | View itemView; 195 | ImageView imageView; 196 | TextView textView; 197 | 198 | StackHolder(@NonNull View itemView) { 199 | super(itemView); 200 | this.itemView = itemView; 201 | imageView = itemView.findViewById(R.id.imageView); 202 | textView = itemView.findViewById(R.id.textView); 203 | } 204 | } 205 | } 206 | 207 | private void resetRandom() { 208 | mRandomPosition = Math.abs(new Random().nextInt() % mStackCount); 209 | selectItems[0] = getResources().getString(R.string.smooth_scroll) + mRandomPosition; 210 | selectItems[1] = getResources().getString(R.string.scroll) + mRandomPosition; 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /sample/src/main/res/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/.DS_Store -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_star_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/drawable/image1.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/drawable/image2.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/drawable/image3.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/image4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/drawable/image4.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/image5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/drawable/image5.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/image6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/drawable/image6.jpg -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 24 | 25 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/image_card.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 14 | 15 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LittleMango/StackLayoutManager/334e6d26bde127f39c7fc506bc639d3bf3bab76e/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values-zh-rCN/arrays.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 卡片从左到右移出 7 | 卡片从右到左移出 8 | 卡片从上到下移出 9 | 卡片从下到上移出 10 | 改变PageMode 11 | 增加每个item之间的偏移 12 | 减少每个item之间的偏移 13 | 增加可见item的数量 14 | 减少可见item的数量 15 | 增加PageMode=true时的滑动阻尼 16 | 减少PageMode=true时的滑动阻尼 17 | 自定义动画 18 | 19 | -------------------------------------------------------------------------------- /sample/src/main/res/values-zh-rCN/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | StackLayoutManagerMaster 3 | 带动画滚动到指定位置 4 | 不带动画滚动到指定位置 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values/arrays.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | From Left To Right 7 | From Right To Left 8 | From Top To Bottom 9 | From Bottom To Top 10 | Change PageMode 11 | Increase item offset 12 | Decrease item offset 13 | Increase visible itemCount 14 | Decrease visible itemCount 15 | Increase fling damp 16 | Decrease fling damp 17 | Use custom animation 18 | 19 | -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | StackLayoutManagerMaster 3 | smoothScroll to 4 | scroll to 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/src/test/java/com/littlemango/stacklayoutmanagermaster/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.littlemango.stacklayoutmanagermaster 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':StackLayoutManager' 2 | --------------------------------------------------------------------------------