├── .gitignore ├── .idea ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zp │ │ └── rcsv │ │ └── sample │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zp │ │ │ └── rcsv │ │ │ └── sample │ │ │ ├── AppInfo.java │ │ │ ├── CardStackActivity.java │ │ │ ├── MainActivity.java │ │ │ └── RCSAdapter.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_card_stack.xml │ │ ├── activity_main.xml │ │ └── rcs_activity_item.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 │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zp │ └── rcsv │ └── sample │ └── ExampleUnitTest.java ├── build.gradle ├── cardstackview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── devilist │ │ │ └── cardstackview │ │ │ ├── CardManager.java │ │ │ ├── CardRecyclerView.java │ │ │ ├── PokerCardManager.java │ │ │ └── TowerCardManager.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── devilist │ └── cardstackview │ └── ExampleUnitTest.java ├── gradle.properties ├── images └── image.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RecyclerCardStackView 2 | 3 | 这是一个用Recyclerview实现的模仿探探的CardStackView。 4 | 5 | 目前有两种card组织形式。 6 | 7 | ![image](https://github.com/devilist/RecyclerCardStackView/raw/master/images/image.gif) 8 | 9 | # 调用方法 10 | 11 | 和正常使用recyclerview步骤一样: 12 | 13 | 14 | 15 | CardRecyclerView crv_list = findViewById(R.id.crv_list); 16 | RCSAdapter adapter = new RCSAdapter(this); 17 | adapter.addData(appInfolist); 18 | crv_list.setAdapter(adapter); 19 | TowerCardManager towerCardManager = new TowerCardManager(this, crv_list); 20 | towerCardManager.setCardOffset(25); // 设置card和card之间的偏移 21 | towerCardManager.setCardElevation(50); // 设置card和card之间的elevation 22 | towerCardManager.setOnCardDragListener(this); // card拖动监听 23 | crv_list.setLayoutManager(towerCardManager); 24 | 25 | 26 | 27 | 28 | 提供了手动drop card的方法: 29 | 30 | 31 | 32 | public void dropCardNoTouch(int orientation) // orientation > 0 向右丢弃,反之向左丢弃 33 | 34 | 35 | 36 | 37 | 拖拽card的监听:CardManager.OnCardDragListener 38 | 39 | 40 | 41 | @Override 42 | public void onDraggingStateChanged(View view, boolean isDragging, boolean isDropped, float offsetX, float offsetY) { 43 | // 这个方法监听card状态变化 44 | isDragging: 当前card是否正在拖拽中(MotionEvent.ACTION_MOVE) 45 | isDropped: 当前card是否已经被丢弃 46 | offsetX,offsetY: 一次完整的拖动释放生命周期过程中card的偏移 47 | 48 | isDragging = false && isDropped == false 代表card处于释放状态 49 | } 50 | 51 | @Override 52 | public void onCardDragging(View view, float offsetX, float offsetY) { 53 | // 这个方法监听card拖动过程中的偏移 isDragging = true 54 | 55 | } 56 | 57 | 58 | 59 | 60 | 布局中使用: 61 | 62 | 63 | 64 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.zp.rcsv.sample" 7 | minSdkVersion 14 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support:recyclerview-v7:26.1.0' 25 | implementation 'com.android.support:cardview-v7:26.1.0' 26 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 27 | testImplementation 'junit:junit:4.12' 28 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 29 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 30 | implementation project(path: ':cardstackview') 31 | 32 | } 33 | -------------------------------------------------------------------------------- /app/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zp/rcsv/sample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zp.rcsv.sample; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zp.rcsv.sample", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/zp/rcsv/sample/AppInfo.java: -------------------------------------------------------------------------------- 1 | package com.zp.rcsv.sample; 2 | 3 | import android.graphics.drawable.Drawable; 4 | 5 | /** 6 | * Created by zengpu on 16/10/29. 7 | */ 8 | 9 | public class AppInfo { 10 | 11 | private String appName; 12 | private Drawable appIcon; 13 | private int versionCode; 14 | private String versionName; 15 | private String packageName; 16 | 17 | public String getAppName() { 18 | return appName; 19 | } 20 | 21 | public void setAppName(String appName) { 22 | this.appName = appName; 23 | } 24 | 25 | public Drawable getAppIcon() { 26 | return appIcon; 27 | } 28 | 29 | public void setAppIcon(Drawable appIcon) { 30 | this.appIcon = appIcon; 31 | } 32 | 33 | public int getVersionCode() { 34 | return versionCode; 35 | } 36 | 37 | public void setVersionCode(int versionCode) { 38 | this.versionCode = versionCode; 39 | } 40 | 41 | public String getVersionName() { 42 | return versionName; 43 | } 44 | 45 | public void setVersionName(String versionName) { 46 | this.versionName = versionName; 47 | } 48 | 49 | public String getPackageName() { 50 | return packageName; 51 | } 52 | 53 | public void setPackageName(String packageName) { 54 | this.packageName = packageName; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/zp/rcsv/sample/CardStackActivity.java: -------------------------------------------------------------------------------- 1 | package com.zp.rcsv.sample; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.content.pm.ApplicationInfo; 6 | import android.content.pm.PackageInfo; 7 | import android.graphics.drawable.Drawable; 8 | import android.os.Bundle; 9 | import android.support.annotation.Nullable; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.view.View; 12 | import android.widget.Toast; 13 | 14 | import com.devilist.cardstackview.CardManager; 15 | import com.devilist.cardstackview.CardRecyclerView; 16 | import com.devilist.cardstackview.PokerCardManager; 17 | import com.devilist.cardstackview.TowerCardManager; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | /** 23 | * Created by zengp on 2017/12/5. 24 | */ 25 | 26 | public class CardStackActivity extends AppCompatActivity implements RCSAdapter.OnItemClickListener, 27 | CardManager.OnCardDragListener { 28 | 29 | private CardRecyclerView crv_list; 30 | 31 | private List appInfolist = new ArrayList<>(); 32 | 33 | public static void start(Context context, String type) { 34 | Intent starter = new Intent(context, CardStackActivity.class); 35 | starter.putExtra("type", type); 36 | context.startActivity(starter); 37 | } 38 | 39 | @Override 40 | protected void onCreate(@Nullable Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_card_stack); 43 | 44 | new Thread(new Runnable() { 45 | @Override 46 | public void run() { 47 | initData(); 48 | runOnUiThread(new Runnable() { 49 | @Override 50 | public void run() { 51 | initView(); 52 | } 53 | }); 54 | } 55 | }).start(); 56 | 57 | } 58 | 59 | private void initView() { 60 | findViewById(R.id.tv_load).setVisibility(View.GONE); 61 | crv_list = findViewById(R.id.crv_list); 62 | RCSAdapter adapter = new RCSAdapter(this); 63 | adapter.addData(appInfolist); 64 | adapter.setOnItemClickListener(this); 65 | crv_list.setAdapter(adapter); 66 | String type = getIntent().getStringExtra("type"); 67 | if (type.equals("tower")) { 68 | TowerCardManager towerCardManager = new TowerCardManager(this, crv_list); 69 | towerCardManager.setCardOffset(25); 70 | towerCardManager.setCardElevation(50); 71 | towerCardManager.setOnCardDragListener(this); 72 | crv_list.setLayoutManager(towerCardManager); 73 | } else if (type.equals("poker")) { 74 | PokerCardManager pokerCardManager = new PokerCardManager(this, crv_list); 75 | pokerCardManager.setCardOffset(25); 76 | pokerCardManager.setCardElevation(50); 77 | pokerCardManager.setOnCardDragListener(this); 78 | crv_list.setLayoutManager(pokerCardManager); 79 | } 80 | } 81 | 82 | private void initData() { 83 | 84 | List packages = getPackageManager().getInstalledPackages(0); 85 | 86 | for (int i = 0; i < packages.size(); i++) { 87 | 88 | PackageInfo packageInfo = packages.get(i); 89 | String appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString(); 90 | Drawable appIcon = packageInfo.applicationInfo.loadIcon(getPackageManager()); 91 | int versionCode = packageInfo.versionCode; 92 | String versionName = packageInfo.versionName; 93 | String packageName = packageInfo.packageName; 94 | 95 | AppInfo appInfo = new AppInfo(); 96 | appInfo.setAppName(appName); 97 | appInfo.setAppIcon(appIcon); 98 | appInfo.setVersionCode(versionCode); 99 | appInfo.setVersionName(versionName); 100 | appInfo.setPackageName(packageName); 101 | 102 | if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { 103 | appInfolist.add(appInfo); 104 | } 105 | } 106 | } 107 | 108 | @Override 109 | public void onDraggingStateChanged(View view, boolean isDragging, boolean isDropped, float offsetX, float offsetY) { 110 | 111 | } 112 | 113 | @Override 114 | public void onCardDragging(View view, float offsetX, float offsetY) { 115 | 116 | } 117 | 118 | @Override 119 | public void onItemClick(View view, int position) { 120 | Toast.makeText(this, "onItemClick", Toast.LENGTH_SHORT).show(); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /app/src/main/java/com/zp/rcsv/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zp.rcsv.sample; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | import com.devilist.cardstackview.CardRecyclerView; 8 | 9 | public class MainActivity extends AppCompatActivity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | 16 | findViewById(R.id.tv_tower).setOnClickListener(new View.OnClickListener() { 17 | @Override 18 | public void onClick(View v) { 19 | CardStackActivity.start(MainActivity.this, "tower"); 20 | } 21 | }); 22 | findViewById(R.id.tv_poker).setOnClickListener(new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | CardStackActivity.start(MainActivity.this, "poker"); 26 | } 27 | }); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/zp/rcsv/sample/RCSAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zp.rcsv.sample; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.CardView; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | import com.devilist.cardstackview.CardRecyclerView; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * Created by zengpu on 2016/10/28. 19 | */ 20 | 21 | public class RCSAdapter extends CardRecyclerView.CardAdapter { 22 | 23 | private Context context; 24 | private List appInfolist = new ArrayList<>(); 25 | private List tempData = new ArrayList<>(); 26 | private OnItemClickListener mOnItemClickListener; 27 | 28 | 29 | public RCSAdapter(Context mContext) { 30 | this.context = mContext; 31 | } 32 | 33 | public void addData(List data) { 34 | this.appInfolist = data; 35 | notifyDataSetChanged(); 36 | } 37 | 38 | @Override 39 | public int getVisibleCardCount() { 40 | return 3; 41 | } 42 | 43 | @Override 44 | protected void delItem(int position) { 45 | if (null != appInfolist && appInfolist.size() > 0) { 46 | appInfolist.remove(position); 47 | notifyItemRemoved(position); 48 | } 49 | } 50 | 51 | @Override 52 | protected void recycleData() { 53 | if (isEnableDataRecycle()) { 54 | if (appInfolist.size() > getVisibleCardCount() + 1) { 55 | tempData.add(appInfolist.get(0)); 56 | delItem(0); 57 | } else { 58 | tempData.add(appInfolist.get(0)); 59 | appInfolist.remove(0); 60 | notifyItemRemoved(0); 61 | int start = appInfolist.size(); 62 | appInfolist.addAll(tempData); 63 | notifyItemRangeInserted(start, tempData.size()); 64 | // notifyDataSetChanged(); 65 | tempData.clear(); 66 | } 67 | } 68 | } 69 | 70 | @Override 71 | protected boolean isEnableDataRecycle() { 72 | return true; 73 | } 74 | 75 | @Override 76 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 77 | View v = LayoutInflater.from(context).inflate(R.layout.rcs_activity_item, parent, false); 78 | return new ViewHolder(v, mOnItemClickListener); 79 | } 80 | 81 | @Override 82 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 83 | AppInfo appInfo = appInfolist.get(position); 84 | ViewHolder mViewHolder = (ViewHolder) holder; 85 | mViewHolder.appNameTv.setText(appInfo.getAppName()); 86 | mViewHolder.appIconIv.setImageDrawable(appInfo.getAppIcon()); 87 | mViewHolder.appVNameTv.setText("v" + appInfo.getVersionName()); 88 | mViewHolder.appPnameTv.setText(appInfo.getPackageName()); 89 | } 90 | 91 | @Override 92 | public int getItemCount() { 93 | return appInfolist.size(); 94 | } 95 | 96 | public interface OnItemClickListener { 97 | void onItemClick(View view, int position); 98 | } 99 | 100 | public void setOnItemClickListener(OnItemClickListener listener) { 101 | mOnItemClickListener = listener; 102 | } 103 | 104 | public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 105 | 106 | private CardView rootCv; 107 | private ImageView appIconIv; 108 | private TextView appNameTv; 109 | private TextView appVCodeTv; 110 | private TextView appVNameTv; 111 | private TextView appPnameTv; 112 | private OnItemClickListener mListener; 113 | 114 | public ViewHolder(View itemView, OnItemClickListener listener) { 115 | super(itemView); 116 | mListener = listener; 117 | 118 | rootCv = (CardView) itemView.findViewById(R.id.cv_view); 119 | appIconIv = (ImageView) itemView.findViewById(R.id.iv_app_icon); 120 | appNameTv = (TextView) itemView.findViewById(R.id.tv_app_name); 121 | appVNameTv = (TextView) itemView.findViewById(R.id.tv_app_version_name); 122 | appPnameTv = (TextView) itemView.findViewById(R.id.tv_app_package_name); 123 | 124 | rootCv.setOnClickListener(this); 125 | } 126 | 127 | @Override 128 | public void onClick(View v) { 129 | mListener.onItemClick(v, getLayoutPosition()); 130 | } 131 | } 132 | } 133 | 134 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/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 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_card_stack.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 17 | 18 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 20 | 21 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/res/layout/rcs_activity_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 20 | 21 | 27 | 28 | 38 | 39 | 49 | 50 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RecyclerCardStackVIew 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/zp/rcsv/sample/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.zp.rcsv.sample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.4' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /cardstackview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /cardstackview/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 26 5 | 6 | defaultConfig { 7 | minSdkVersion 14 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | 14 | } 15 | 16 | buildTypes { 17 | debug { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(dir: 'libs', include: ['*.jar']) 33 | implementation 'com.android.support:appcompat-v7:26.1.0' 34 | implementation 'com.android.support:recyclerview-v7:26.1.0' 35 | 36 | } 37 | -------------------------------------------------------------------------------- /cardstackview/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 | -------------------------------------------------------------------------------- /cardstackview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /cardstackview/src/main/java/com/devilist/cardstackview/CardManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 zengpu 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.devilist.cardstackview; 19 | 20 | import android.content.Context; 21 | import android.support.v7.widget.RecyclerView; 22 | import android.view.MotionEvent; 23 | import android.view.View; 24 | import android.view.ViewGroup; 25 | import android.view.WindowManager; 26 | 27 | 28 | /** 29 | * a helper to handle the drag event of the {@link CardRecyclerView}'s child views; 30 | * Created by zengp on 2017/9/30. 31 | */ 32 | 33 | public abstract class CardManager extends RecyclerView.LayoutManager implements View.OnTouchListener { 34 | 35 | protected int mScreenWidth; 36 | protected int mScreenHeight; 37 | protected CardRecyclerView mRecyclerView; 38 | 39 | protected OnCardDragListener mListener; 40 | 41 | public CardManager(Context context, CardRecyclerView recyclerView) { 42 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 43 | mScreenWidth = wm.getDefaultDisplay().getWidth(); 44 | mScreenHeight = wm.getDefaultDisplay().getHeight(); 45 | mRecyclerView = recyclerView; 46 | } 47 | 48 | @Override 49 | public RecyclerView.LayoutParams generateDefaultLayoutParams() { 50 | return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 51 | RecyclerView.LayoutParams.WRAP_CONTENT); 52 | } 53 | 54 | public abstract void onLayoutCards(RecyclerView.Recycler recycler, RecyclerView.State state); 55 | 56 | public void dropCardNoTouch(int orientation) { 57 | } 58 | 59 | @Override 60 | public final void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { 61 | 62 | if (getItemCount() <= 0) { 63 | removeAndRecycleAllViews(recycler); 64 | return; 65 | } 66 | 67 | // remove all attached child views 68 | detachAndScrapAttachedViews(recycler); 69 | 70 | // re-layout 71 | onLayoutCards(recycler, state); 72 | 73 | } 74 | 75 | @Override 76 | public boolean onTouch(View v, MotionEvent event) { 77 | return false; 78 | } 79 | 80 | public void setOnCardDragListener(OnCardDragListener listener) { 81 | this.mListener = listener; 82 | } 83 | 84 | public interface OnCardDragListener { 85 | 86 | void onDraggingStateChanged(View view, boolean isDragging, boolean isDropped, float offsetX, float offsetY); 87 | 88 | void onCardDragging(View view, float offsetX, float offsetY); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /cardstackview/src/main/java/com/devilist/cardstackview/CardRecyclerView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 zengpu 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.devilist.cardstackview; 19 | 20 | import android.content.Context; 21 | import android.support.annotation.Nullable; 22 | import android.support.v7.widget.RecyclerView; 23 | import android.util.AttributeSet; 24 | 25 | 26 | /** 27 | * Created by zengp on 2017/09/30. 28 | */ 29 | 30 | public class CardRecyclerView extends RecyclerView { 31 | 32 | public CardRecyclerView(Context context) { 33 | this(context, null); 34 | } 35 | 36 | public CardRecyclerView(Context context, @Nullable AttributeSet attrs) { 37 | this(context, attrs, 0); 38 | } 39 | 40 | public CardRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { 41 | super(context, attrs, defStyle); 42 | } 43 | 44 | public int getVisibleCardCount() { 45 | if (getAdapter().getVisibleCardCount() <= 0) 46 | throw new IllegalArgumentException("visibleCardCount must be over zero !!!"); 47 | return getAdapter().getVisibleCardCount(); 48 | } 49 | 50 | @Override 51 | public void setAdapter(Adapter adapter) { 52 | if (adapter instanceof CardAdapter) 53 | super.setAdapter(adapter); 54 | else 55 | throw new IllegalArgumentException(""); 56 | } 57 | 58 | @Override 59 | public CardAdapter getAdapter() { 60 | return (CardAdapter) super.getAdapter(); 61 | } 62 | 63 | @Override 64 | public void setLayoutManager(LayoutManager layout) { 65 | if (layout instanceof CardManager) 66 | super.setLayoutManager(layout); 67 | else 68 | throw new IllegalArgumentException(""); 69 | } 70 | 71 | @Override 72 | public CardManager getLayoutManager() { 73 | return (CardManager) super.getLayoutManager(); 74 | } 75 | 76 | public void dropCard(int orientation) { 77 | if (getChildCount() > 0) 78 | getLayoutManager().dropCardNoTouch(orientation); 79 | } 80 | 81 | protected void dropCard() { 82 | if (getAdapter().isEnableDataRecycle()) { 83 | getAdapter().recycleData(); 84 | } else 85 | getAdapter().delItem(0); 86 | } 87 | 88 | public static abstract class CardAdapter extends Adapter { 89 | 90 | protected abstract void delItem(int position); 91 | 92 | protected abstract void recycleData(); 93 | 94 | public int getVisibleCardCount() { 95 | return 3; 96 | } 97 | 98 | protected boolean isEnableDataRecycle() { 99 | return false; 100 | } 101 | 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /cardstackview/src/main/java/com/devilist/cardstackview/PokerCardManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 zengpu 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.devilist.cardstackview; 19 | 20 | import android.animation.Animator; 21 | import android.animation.AnimatorListenerAdapter; 22 | import android.animation.ValueAnimator; 23 | import android.content.Context; 24 | import android.graphics.Rect; 25 | import android.support.v4.view.ViewCompat; 26 | import android.support.v7.widget.RecyclerView; 27 | import android.view.MotionEvent; 28 | import android.view.VelocityTracker; 29 | import android.view.View; 30 | import android.view.ViewGroup; 31 | import android.view.animation.OvershootInterpolator; 32 | 33 | 34 | /** 35 | * Created by zengp on 2017/09/30. 36 | */ 37 | public class PokerCardManager extends CardManager { 38 | 39 | private float mTouchDownX = 0, mTouchDownY = 0; 40 | private boolean mIsTouchUp = false; 41 | // the dragged border position to differentiate whether the dragged card be drop or reset 42 | private float mDragThresholdX, mDragThresholdY; 43 | private int mMinVelocityThreshold = 2000; 44 | private int mMaxVelocityThreshold = 4500; 45 | 46 | private VelocityTracker mVelocityTracker = null; 47 | 48 | private int mCardOffset = 10; 49 | private int mCardElevation = 10; 50 | 51 | public PokerCardManager(Context context, CardRecyclerView recyclerView) { 52 | super(context, recyclerView); 53 | // forbidden the default item animator 54 | mRecyclerView.setItemAnimator(null); 55 | mDragThresholdX = mScreenWidth / 3; 56 | mDragThresholdY = mScreenHeight / 3; 57 | ViewCompat.setTranslationZ(mRecyclerView, -1); 58 | } 59 | 60 | public void setCardOffset(int cardOffset) { 61 | if (cardOffset < 0) 62 | throw new IllegalArgumentException("cardOffset must be over zero !!!"); 63 | this.mCardOffset = cardOffset; 64 | } 65 | 66 | public void setCardElevation(int cardElevation) { 67 | if (cardElevation < 0) 68 | throw new IllegalArgumentException("cardElevation must be over zero !!!"); 69 | this.mCardElevation = cardElevation; 70 | } 71 | 72 | 73 | @Override 74 | public void onLayoutCards(RecyclerView.Recycler recycler, RecyclerView.State state) { 75 | onLayoutCards(recycler, state, mRecyclerView.getVisibleCardCount(), mCardOffset, mCardElevation); 76 | setTargetDragCard(); 77 | } 78 | 79 | private void onLayoutCards(RecyclerView.Recycler recycler, RecyclerView.State state, 80 | int visibleCardCount, int cardOffset, int cardElevation) { 81 | if (getItemCount() > 0) { 82 | // calculate the validate areas that all the visible cards cover. 83 | int left = 0; 84 | int top = 0; 85 | // in order to compat lower version, card must be added from last to first, 86 | // and if all the cards are added at every time this method invoked, there may 87 | // be some performance problems happened, such as OOM crash, long time wasted to 88 | // attached cards,and less smoothly when scroll etc.So to avoid these potential 89 | // problems, only necessary cards will be attached to improve performance 90 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 91 | for (int i = maxAttachChildrenCount; i >= 0; i--) { 92 | View child_i = recycler.getViewForPosition(i); 93 | addView(child_i); 94 | measureChildWithMargins(child_i, 0, 0); 95 | int childWidth = getDecoratedMeasuredWidth(child_i); 96 | int childHeight = getDecoratedMeasuredHeight(child_i); 97 | if (i == maxAttachChildrenCount) { 98 | int childWidthWithTotalOffset = childWidth + cardOffset * (visibleCardCount - 1); 99 | int childHeightWithTotalOffset = childHeight + cardOffset * (visibleCardCount - 1); 100 | int parentWExcludePadding = getWidth() - getPaddingLeft() - getPaddingRight(); 101 | int parentHExcludePadding = getHeight() - getPaddingTop() - getPaddingBottom(); 102 | ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child_i.getLayoutParams(); 103 | int childMarginHorizOffset = params.leftMargin - params.rightMargin; 104 | int childMarginVertOffset = params.topMargin - params.bottomMargin; 105 | left = (parentWExcludePadding - childWidthWithTotalOffset) / 2 + getPaddingLeft() + childMarginHorizOffset; 106 | top = (parentHExcludePadding - childHeightWithTotalOffset) / 2 + getPaddingTop() + childMarginVertOffset; 107 | } 108 | // remove decorator area 109 | Rect childRect = new Rect(); 110 | calculateItemDecorationsForChild(child_i, childRect); 111 | int left_i, top_i; 112 | if (i <= visibleCardCount - 1) { 113 | left_i = left + cardOffset * i; 114 | top_i = top + cardOffset * (visibleCardCount - 1 - i); 115 | // set elevations for all the visible children 116 | ViewCompat.setTranslationZ(child_i, cardElevation * (visibleCardCount - i)); 117 | } else { 118 | left_i = left + cardOffset * (visibleCardCount - 1); 119 | top_i = top; 120 | ViewCompat.setTranslationZ(child_i, 0); 121 | } 122 | // reset card 123 | child_i.setTranslationY(0); 124 | child_i.setTranslationX(0); 125 | child_i.setAlpha(1f); 126 | child_i.setRotation(0); 127 | child_i.setScaleX(1f); 128 | child_i.setScaleY(1f); 129 | layoutDecorated(child_i, left_i, top_i, left_i + childWidth, top_i + childHeight); 130 | } 131 | } 132 | } 133 | 134 | private void setTargetDragCard() { 135 | // count of all the attached cards 136 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 137 | if (maxAttachChildrenCount >= 0) { 138 | for (int i = 0; i <= maxAttachChildrenCount; i++) { 139 | getChildAt(i).setOnTouchListener(this); 140 | if (i == maxAttachChildrenCount) { 141 | dispatchOnDragEvent(getChildAt(i), false, false, 0, 0); 142 | } 143 | } 144 | } 145 | } 146 | 147 | @Override 148 | public boolean onTouch(View v, MotionEvent event) { 149 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 150 | if (v != getChildAt(maxAttachChildrenCount)) 151 | return true; 152 | if (mVelocityTracker == null) { 153 | mVelocityTracker = VelocityTracker.obtain(); 154 | } 155 | mVelocityTracker.addMovement(event); 156 | int velocityX = 0; 157 | int velocityY = 0; 158 | if (event.getAction() == MotionEvent.ACTION_DOWN) { 159 | ViewCompat.setTranslationZ(mRecyclerView, 1); 160 | mTouchDownX = event.getRawX(); 161 | mTouchDownY = event.getRawY(); 162 | mIsTouchUp = false; 163 | } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 164 | if (mIsTouchUp) { 165 | mTouchDownX = event.getRawX(); 166 | mTouchDownY = event.getRawY(); 167 | mIsTouchUp = false; 168 | } 169 | ViewCompat.setTranslationZ(mRecyclerView, 1); 170 | if (event.getEventTime() - event.getDownTime() >= 100) { 171 | dragCard(v, event.getRawX() - mTouchDownX, event.getRawY() - mTouchDownY, 172 | event.getRawX() - mTouchDownX); 173 | dispatchOnDragEvent(v, true, false, event.getRawX() - mTouchDownX, event.getRawY() - mTouchDownY); 174 | return true; 175 | } 176 | } else if (event.getAction() == MotionEvent.ACTION_UP) { 177 | mIsTouchUp = true; 178 | if (mVelocityTracker != null) { 179 | mVelocityTracker.computeCurrentVelocity(1000); 180 | velocityX = (int) mVelocityTracker.getXVelocity(); 181 | velocityY = (int) mVelocityTracker.getYVelocity(); 182 | } 183 | int velocity = (int) Math.sqrt(velocityX * velocityX + velocityY * velocityY); 184 | if (velocity > mMaxVelocityThreshold && event.getEventTime() - event.getDownTime() < 100) { 185 | ViewCompat.setTranslationZ(mRecyclerView, -1); 186 | return true; 187 | } else if (velocity >= mMinVelocityThreshold 188 | || event.getEventTime() - event.getDownTime() >= 100) { 189 | releaseCard(v, event.getRawX() - mTouchDownX, event.getRawY() - mTouchDownY, velocity); 190 | return true; 191 | } else 192 | ViewCompat.setTranslationZ(mRecyclerView, -1); 193 | } 194 | return false; 195 | } 196 | 197 | // hold the card just dragging as free as you can 198 | private void dragCard(View card, float offset_x, float offset_y, float touchX) { 199 | // trans 200 | card.setTranslationX(offset_x); 201 | card.setTranslationY(offset_y); 202 | // factor 203 | float distance = (float) Math.sqrt(offset_x * offset_x + offset_y * offset_y); 204 | float maxDistance = (float) Math.sqrt(mScreenWidth * mScreenWidth + mScreenHeight * mScreenHeight); 205 | float factor = Math.min(1, distance / maxDistance); 206 | // tansZ 207 | float ori_elevation = mRecyclerView.getVisibleCardCount() * mCardElevation; 208 | ViewCompat.setTranslationZ(card, (float) (ori_elevation * (1 + Math.sqrt(factor)))); 209 | //scale 210 | card.setScaleX(1 - factor); 211 | card.setScaleY(1 - factor); 212 | // alpha 213 | card.setAlpha(1 - factor * factor); 214 | // rotate 215 | float rotateDegree = offset_x == 0 ? 0 : (float) Math.asin(touchX / mScreenWidth); 216 | rotateDegree = (float) (rotateDegree * 180 / Math.PI); 217 | // deg factor to make the rotate more smooth 218 | // rotateDegree = rotateDegree * factor; 219 | card.setRotation(rotateDegree); 220 | refreshOtherVisibleCardsPosition(offset_x, offset_y); 221 | } 222 | 223 | private void releaseCard(View card, float offset_x, float offset_y, int velocity) { 224 | // check card status to decide next action 225 | if (Math.abs(offset_x) >= mDragThresholdX 226 | || Math.abs(offset_y) >= mDragThresholdY 227 | || (velocity >= mMinVelocityThreshold && velocity <= mMaxVelocityThreshold)) { 228 | dropCard(card); 229 | } else { 230 | resetDragCard(card); 231 | } 232 | } 233 | 234 | // drop card without ontouch event drop from left if orientation is minus otherwise right 235 | @Override 236 | public void dropCardNoTouch(int orientation) { 237 | if (getChildCount() <= 0) 238 | return; 239 | ViewCompat.setTranslationZ(mRecyclerView, 1); 240 | final View card = getChildAt(getChildCount() - 1); 241 | orientation = orientation < 0 ? -1 : 1; 242 | ValueAnimator animator = ValueAnimator.ofFloat(0, 1.1f); 243 | final int finalOrientation = orientation; 244 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 245 | @Override 246 | public void onAnimationUpdate(ValueAnimator animation) { 247 | float offset = (float) animation.getAnimatedValue(); 248 | dragCard(card, finalOrientation * mDragThresholdX * offset, mDragThresholdY * offset / 5, 249 | finalOrientation * mScreenWidth / 2 * offset); 250 | dispatchOnDragEvent(card, true, false, card.getTranslationX(), card.getTranslationY()); 251 | } 252 | }); 253 | animator.addListener(new AnimatorListenerAdapter() { 254 | @Override 255 | public void onAnimationEnd(Animator animation) { 256 | dropCard(card); 257 | } 258 | }); 259 | animator.setDuration(150); 260 | animator.start(); 261 | } 262 | 263 | // del the current card or recycle it 264 | private void dropCard(final View card) { 265 | final float oriTransX = card.getTranslationX(); 266 | final float oriTransY = card.getTranslationY(); 267 | final float oriRotateDeg = card.getRotation(); 268 | final float oriScaleX = card.getScaleX(); 269 | final float oriScaleY = card.getScaleY(); 270 | final float oriAlpha = card.getAlpha(); 271 | 272 | ValueAnimator animator = ValueAnimator.ofFloat(1, 0); 273 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 274 | @Override 275 | public void onAnimationUpdate(ValueAnimator animation) { 276 | float offset = (float) animation.getAnimatedValue(); 277 | card.setTranslationX(3 * oriTransX - 2 * oriTransX * offset); 278 | card.setTranslationY(3 * oriTransY - 2 * oriTransY * offset); 279 | card.setRotation(3 * oriRotateDeg - 2 * oriRotateDeg * offset); 280 | card.setScaleX(oriScaleX * offset); 281 | card.setScaleY(oriScaleY * offset); 282 | card.setAlpha(oriAlpha * offset); 283 | refreshOtherVisibleCardsPosition(mDragThresholdX + (Math.abs(oriTransX) - mDragThresholdX) * offset, 284 | mDragThresholdY + (Math.abs(oriTransY) - mDragThresholdY) * offset); 285 | dispatchOnDragEvent(card, false, false, oriTransX, oriTransY); 286 | } 287 | }); 288 | animator.addListener(new AnimatorListenerAdapter() { 289 | @Override 290 | public void onAnimationEnd(Animator animation) { 291 | if (null != mRecyclerView && null != mRecyclerView.getAdapter()) { 292 | mRecyclerView.dropCard(); 293 | dispatchOnDragEvent(card, false, true, oriTransX, oriTransY); 294 | ViewCompat.setTranslationZ(mRecyclerView, -1); 295 | } 296 | } 297 | }); 298 | animator.setDuration(300); 299 | animator.start(); 300 | } 301 | 302 | // let the card go back to its ori position 303 | private void resetDragCard(final View card) { 304 | final float oriTransX = card.getTranslationX(); 305 | final float oriTransY = card.getTranslationY(); 306 | final float oriRotateDeg = card.getRotation(); 307 | final float oriScaleX = card.getScaleX(); 308 | final float oriScaleY = card.getScaleY(); 309 | final float oriAlpha = card.getAlpha(); 310 | 311 | final float oriTransZ = ViewCompat.getTranslationZ(card); 312 | final float targetTransZ = mRecyclerView.getVisibleCardCount() * mCardElevation; 313 | 314 | ValueAnimator animator = ValueAnimator.ofFloat(1, 0); 315 | animator.setInterpolator(new OvershootInterpolator()); 316 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 317 | @Override 318 | public void onAnimationUpdate(ValueAnimator animation) { 319 | float offset = (float) animation.getAnimatedValue(); 320 | card.setTranslationX(oriTransX * offset); 321 | card.setTranslationY(oriTransY * offset); 322 | ViewCompat.setTranslationZ(card, targetTransZ + (oriTransZ - targetTransZ) * offset); 323 | card.setRotation(oriRotateDeg * offset); 324 | card.setScaleX((oriScaleX - 1) * offset + 1); 325 | card.setScaleY((oriScaleY - 1) * offset + 1); 326 | card.setAlpha((oriAlpha - 1) * offset + 1); 327 | refreshOtherVisibleCardsPosition(oriTransX * offset, oriTransY * offset); 328 | dispatchOnDragEvent(card, false, false, oriTransX * offset, oriTransY * offset); 329 | 330 | } 331 | }); 332 | animator.addListener(new AnimatorListenerAdapter() { 333 | @Override 334 | public void onAnimationEnd(Animator animation) { 335 | ViewCompat.setTranslationZ(mRecyclerView, -1); 336 | } 337 | }); 338 | animator.setDuration(500); 339 | animator.start(); 340 | } 341 | 342 | // refresh other visible cards' positions when dragging 343 | private void refreshOtherVisibleCardsPosition(float offset_x, float offset_y) { 344 | float factor = (float) (Math.sqrt(offset_x * offset_x + offset_y * offset_y) 345 | / Math.sqrt(mDragThresholdX * mDragThresholdX + mDragThresholdY * mDragThresholdY)); 346 | factor = Math.min(factor, 1); 347 | if (getItemCount() > 1) { 348 | int cardOffset = mCardOffset; 349 | // count of all the attached cards 350 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 351 | // count of all the cards required refreshing 352 | int totalRefreshingCount = Math.min(getItemCount() - 1, mRecyclerView.getVisibleCardCount()); 353 | for (int i = 1; i <= totalRefreshingCount; i++) { 354 | int childPosition = maxAttachChildrenCount - i; 355 | // trans x y. if the visible cards count is three, for example, 356 | // here we only need to handle translation x and y for the other two visible cards at position 1 and 2 357 | if (i < totalRefreshingCount) { 358 | getChildAt(childPosition).setTranslationX(-cardOffset * factor); 359 | getChildAt(childPosition).setTranslationY(cardOffset * factor); 360 | } 361 | 362 | // it is different for handling transZ compared to transX Y. 363 | // we just need to handle more than one card. 364 | 365 | // calculate the current card ori elevation 366 | int current = mRecyclerView.getVisibleCardCount() - i; 367 | int oriElevation = mCardElevation * current; 368 | // update 369 | int currentElevation = (int) (oriElevation + mCardElevation * factor); 370 | ViewCompat.setTranslationZ(getChildAt(childPosition), currentElevation); 371 | } 372 | } 373 | } 374 | 375 | private void dispatchOnDragEvent(View view, boolean isDragging, boolean isDropped, 376 | float offsetX, float offsetY) { 377 | if (null != mListener) { 378 | mListener.onDraggingStateChanged(view, isDragging, isDropped, offsetX, offsetY); 379 | if (isDragging) 380 | mListener.onCardDragging(view, offsetX, offsetY); 381 | } 382 | } 383 | } 384 | -------------------------------------------------------------------------------- /cardstackview/src/main/java/com/devilist/cardstackview/TowerCardManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 zengpu 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.devilist.cardstackview; 19 | 20 | import android.animation.Animator; 21 | import android.animation.AnimatorListenerAdapter; 22 | import android.animation.ValueAnimator; 23 | import android.content.Context; 24 | import android.graphics.Rect; 25 | import android.support.v4.view.ViewCompat; 26 | import android.support.v7.widget.RecyclerView; 27 | import android.view.MotionEvent; 28 | import android.view.VelocityTracker; 29 | import android.view.View; 30 | import android.view.ViewGroup; 31 | import android.view.animation.OvershootInterpolator; 32 | 33 | 34 | /** 35 | * 36 | * Created by zengp on 2017/11/19. 37 | */ 38 | 39 | public class TowerCardManager extends CardManager { 40 | 41 | private float mTouchDownX = 0; 42 | private float mTouchDownY = 0; 43 | private boolean mIsTouchUp = false; 44 | // the dragged border position to differentiate whether the dragged card be drop or reset 45 | private float mDragThresholdX, mDragThresholdY; 46 | private int mMinVelocityThreshold = 2000; 47 | private int mMaxVelocityThreshold = 4500; 48 | 49 | private VelocityTracker mVelocityTracker = null; 50 | 51 | private int mCardOffset = 10; 52 | private int mCardElevation = 10; 53 | 54 | public TowerCardManager(Context context, CardRecyclerView recyclerView) { 55 | super(context, recyclerView); 56 | // forbidden the default item animator 57 | // ((DefaultItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 58 | mRecyclerView.setItemAnimator(null); 59 | mDragThresholdX = mScreenWidth / 3; 60 | mDragThresholdY = mScreenHeight / 3; 61 | ViewCompat.setTranslationZ(mRecyclerView, -1); 62 | } 63 | 64 | public void setCardOffset(int cardOffset) { 65 | if (cardOffset < 0) 66 | throw new IllegalArgumentException("cardOffset must be over zero !!!"); 67 | this.mCardOffset = cardOffset; 68 | } 69 | 70 | public void setCardElevation(int cardElevation) { 71 | if (cardElevation < 0) 72 | throw new IllegalArgumentException("cardElevation must be over zero !!!"); 73 | this.mCardElevation = cardElevation; 74 | } 75 | 76 | 77 | @Override 78 | public void onLayoutCards(RecyclerView.Recycler recycler, RecyclerView.State state) { 79 | onLayoutCards(recycler, state, mRecyclerView.getVisibleCardCount(), mCardOffset, mCardElevation); 80 | setTargetDragCard(); 81 | } 82 | 83 | private void onLayoutCards(RecyclerView.Recycler recycler, RecyclerView.State state, 84 | int visibleCardCount, int cardOffset, int cardElevation) { 85 | if (getItemCount() > 0) { 86 | // calculate the validate areas that all the visible cards cover. 87 | int left = 0; 88 | int top = 0; 89 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 90 | for (int i = maxAttachChildrenCount; i >= 0; i--) { 91 | View child_i = recycler.getViewForPosition(i); 92 | addView(child_i); 93 | measureChildWithMargins(child_i, 0, 0); 94 | int childWidth = getDecoratedMeasuredWidth(child_i); 95 | int childHeight = getDecoratedMeasuredHeight(child_i); 96 | if (i == maxAttachChildrenCount) { 97 | int childHeightWithTotalOffset = childHeight + cardOffset * (visibleCardCount - 1); 98 | int parentWExcludePadding = getWidth() - getPaddingLeft() - getPaddingRight(); 99 | int parentHExcludePadding = getHeight() - getPaddingTop() - getPaddingBottom(); 100 | ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child_i.getLayoutParams(); 101 | int childMarginHorizOffset = params.leftMargin - params.rightMargin; 102 | int childMarginVertOffset = params.topMargin - params.bottomMargin; 103 | left = (parentWExcludePadding - childWidth) / 2 + getPaddingLeft() + childMarginHorizOffset; 104 | top = (parentHExcludePadding - childHeightWithTotalOffset) / 2 + getPaddingTop() + childMarginVertOffset; 105 | } 106 | // remove decorator area 107 | Rect childRect = new Rect(); 108 | calculateItemDecorationsForChild(child_i, childRect); 109 | int top_i; 110 | float scaleX = (childWidth - i * cardOffset * 2) * 1f / childWidth; 111 | float scaleY = (childHeight - i * cardOffset * 2) * 1f / childHeight; 112 | float transZ = cardElevation * (visibleCardCount - i); 113 | if (i <= visibleCardCount - 1) { 114 | top_i = top + cardOffset * i * 2; 115 | } else { 116 | top_i = top + cardOffset * (visibleCardCount - 1) * 2; 117 | } 118 | child_i.setScaleX(scaleX); 119 | child_i.setScaleY(scaleY); 120 | // set elevations for all the visible children 121 | ViewCompat.setTranslationZ(child_i, transZ); 122 | 123 | // reset card 124 | child_i.setTranslationY(0); 125 | child_i.setTranslationX(0); 126 | child_i.setAlpha(1f); 127 | child_i.setRotation(0); 128 | 129 | layoutDecorated(child_i, left, top_i, left + childWidth, top_i + childHeight); 130 | } 131 | } 132 | } 133 | 134 | private void setTargetDragCard() { 135 | // count of all the attached cards 136 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 137 | if (maxAttachChildrenCount >= 0) { 138 | for (int i = 0; i <= maxAttachChildrenCount; i++) { 139 | getChildAt(i).setOnTouchListener(this); 140 | if (i == maxAttachChildrenCount) { 141 | dispatchOnDragEvent(getChildAt(i), false, false, 0, 0); 142 | } 143 | } 144 | } 145 | } 146 | 147 | @Override 148 | public boolean onTouch(View v, MotionEvent event) { 149 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 150 | if (v != getChildAt(maxAttachChildrenCount)) 151 | return true; 152 | if (mVelocityTracker == null) { 153 | mVelocityTracker = VelocityTracker.obtain(); 154 | } 155 | mVelocityTracker.addMovement(event); 156 | int velocityX = 0; 157 | int velocityY = 0; 158 | if (event.getAction() == MotionEvent.ACTION_DOWN) { 159 | ViewCompat.setTranslationZ(mRecyclerView, 1); 160 | mTouchDownX = event.getRawX(); 161 | mTouchDownY = event.getRawY(); 162 | mIsTouchUp = false; 163 | } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 164 | if (mIsTouchUp) { 165 | mTouchDownX = event.getRawX(); 166 | mTouchDownY = event.getRawY(); 167 | mIsTouchUp = false; 168 | } 169 | ViewCompat.setTranslationZ(mRecyclerView, 1); 170 | if (event.getEventTime() - event.getDownTime() >= 100) { 171 | dragCard(v, event.getRawX() - mTouchDownX, event.getRawY() - mTouchDownY, 172 | event.getRawX() - mTouchDownX); 173 | dispatchOnDragEvent(v, true, false, event.getRawX() - mTouchDownX, event.getRawY() - mTouchDownY); 174 | return true; 175 | } 176 | } else if (event.getAction() == MotionEvent.ACTION_UP) { 177 | mIsTouchUp = true; 178 | if (mVelocityTracker != null) { 179 | mVelocityTracker.computeCurrentVelocity(1000); 180 | velocityX = (int) mVelocityTracker.getXVelocity(); 181 | velocityY = (int) mVelocityTracker.getYVelocity(); 182 | } 183 | int velocity = (int) Math.sqrt(velocityX * velocityX + velocityY * velocityY); 184 | if (velocity > mMaxVelocityThreshold && event.getEventTime() - event.getDownTime() < 100) { 185 | ViewCompat.setTranslationZ(mRecyclerView, -1); 186 | return true; 187 | } else if (velocity >= mMinVelocityThreshold 188 | || event.getEventTime() - event.getDownTime() >= 100) { 189 | releaseCard(v, event.getRawX() - mTouchDownX, event.getRawY() - mTouchDownY, velocity); 190 | return true; 191 | } else 192 | ViewCompat.setTranslationZ(mRecyclerView, -1); 193 | } 194 | return false; 195 | } 196 | 197 | // hold the card just dragging as free as you can 198 | private void dragCard(View card, float offset_x, float offset_y, float touchX) { 199 | // trans 200 | card.setTranslationX(offset_x); 201 | card.setTranslationY(offset_y); 202 | // factor 203 | float distance = (float) Math.sqrt(offset_x * offset_x + offset_y * offset_y); 204 | float maxDistance = (float) Math.sqrt(mScreenWidth * mScreenWidth + mScreenHeight * mScreenHeight); 205 | float factor = Math.min(1, distance / maxDistance); 206 | // tansZ 207 | float ori_elevation = mRecyclerView.getVisibleCardCount() * mCardElevation; 208 | ViewCompat.setTranslationZ(card, (float) (ori_elevation * (1 + Math.sqrt(factor)))); 209 | //scale 210 | card.setScaleX(1 - factor); 211 | card.setScaleY(1 - factor); 212 | // alpha 213 | card.setAlpha(1 - factor * factor); 214 | // rotate 215 | float rotateDegree = offset_x == 0 ? 0 : (float) Math.asin(touchX / mScreenWidth); 216 | rotateDegree = (float) (rotateDegree * 180 / Math.PI); 217 | // deg factor to make the rotate more smooth 218 | // rotateDegree = rotateDegree * factor; 219 | card.setRotation(rotateDegree); 220 | refreshOtherVisibleCardsPosition(offset_x, offset_y); 221 | } 222 | 223 | private void releaseCard(View card, float offset_x, float offset_y, int velocity) { 224 | // check card status to decide next action 225 | if (Math.abs(offset_x) >= mDragThresholdX 226 | || Math.abs(offset_y) >= mDragThresholdY 227 | || (velocity >= mMinVelocityThreshold && velocity <= mMaxVelocityThreshold)) { 228 | dropCard(card); 229 | } else { 230 | resetDragCard(card); 231 | } 232 | } 233 | 234 | // drop card without ontouch event drop from left if orientation is minus otherwise right 235 | @Override 236 | public void dropCardNoTouch(int orientation) { 237 | if (getChildCount() <= 0) 238 | return; 239 | ViewCompat.setTranslationZ(mRecyclerView, 1); 240 | final View card = getChildAt(getChildCount() - 1); 241 | orientation = orientation < 0 ? -1 : 1; 242 | ValueAnimator animator = ValueAnimator.ofFloat(0, 1.1f); 243 | final int finalOrientation = orientation; 244 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 245 | @Override 246 | public void onAnimationUpdate(ValueAnimator animation) { 247 | float offset = (float) animation.getAnimatedValue(); 248 | dragCard(card, finalOrientation * mDragThresholdX * offset, mDragThresholdY * offset / 5, 249 | finalOrientation * mScreenWidth / 2 * offset); 250 | dispatchOnDragEvent(card, true, false, card.getTranslationX(), card.getTranslationY()); 251 | } 252 | }); 253 | animator.addListener(new AnimatorListenerAdapter() { 254 | @Override 255 | public void onAnimationEnd(Animator animation) { 256 | dropCard(card); 257 | } 258 | }); 259 | animator.setDuration(150); 260 | animator.start(); 261 | } 262 | 263 | // del the current card or recycle it 264 | private void dropCard(final View card) { 265 | final float oriTransX = card.getTranslationX(); 266 | final float oriTransY = card.getTranslationY(); 267 | final float oriRotateDeg = card.getRotation(); 268 | final float oriScaleX = card.getScaleX(); 269 | final float oriScaleY = card.getScaleY(); 270 | final float oriAlpha = card.getAlpha(); 271 | 272 | ValueAnimator animator = ValueAnimator.ofFloat(1, 0); 273 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 274 | @Override 275 | public void onAnimationUpdate(ValueAnimator animation) { 276 | float offset = (float) animation.getAnimatedValue(); 277 | card.setTranslationX(3 * oriTransX - 2 * oriTransX * offset); 278 | card.setTranslationY(3 * oriTransY - 2 * oriTransY * offset); 279 | card.setRotation(3 * oriRotateDeg - 2 * oriRotateDeg * offset); 280 | card.setScaleX(oriScaleX * offset); 281 | card.setScaleY(oriScaleY * offset); 282 | card.setAlpha(oriAlpha * offset); 283 | refreshOtherVisibleCardsPosition(mDragThresholdX + (Math.abs(oriTransX) - mDragThresholdX) * offset, 284 | mDragThresholdY + (Math.abs(oriTransY) - mDragThresholdY) * offset); 285 | dispatchOnDragEvent(card, false, false, oriTransX, oriTransY); 286 | } 287 | }); 288 | animator.addListener(new AnimatorListenerAdapter() { 289 | @Override 290 | public void onAnimationEnd(Animator animation) { 291 | if (null != mRecyclerView && null != mRecyclerView.getAdapter()) { 292 | mRecyclerView.dropCard(); 293 | dispatchOnDragEvent(card, false, true, oriTransX, oriTransY); 294 | ViewCompat.setTranslationZ(mRecyclerView, -1); 295 | } 296 | } 297 | }); 298 | animator.setDuration(300); 299 | animator.start(); 300 | } 301 | 302 | // let the card go back to its ori position 303 | private void resetDragCard(final View card) { 304 | final float oriTransX = card.getTranslationX(); 305 | final float oriTransY = card.getTranslationY(); 306 | final float oriRotateDeg = card.getRotation(); 307 | final float oriScaleX = card.getScaleX(); 308 | final float oriScaleY = card.getScaleY(); 309 | final float oriAlpha = card.getAlpha(); 310 | 311 | final float oriTransZ = ViewCompat.getTranslationZ(card); 312 | final float targetTransZ = mRecyclerView.getVisibleCardCount() * mCardElevation; 313 | 314 | ValueAnimator animator = ValueAnimator.ofFloat(1, 0); 315 | animator.setInterpolator(new OvershootInterpolator()); 316 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 317 | @Override 318 | public void onAnimationUpdate(ValueAnimator animation) { 319 | float offset = (float) animation.getAnimatedValue(); 320 | card.setTranslationX(oriTransX * offset); 321 | card.setTranslationY(oriTransY * offset); 322 | ViewCompat.setTranslationZ(card, targetTransZ + (oriTransZ - targetTransZ) * offset); 323 | card.setRotation(oriRotateDeg * offset); 324 | card.setScaleX((oriScaleX - 1) * offset + 1); 325 | card.setScaleY((oriScaleY - 1) * offset + 1); 326 | card.setAlpha((oriAlpha - 1) * offset + 1); 327 | refreshOtherVisibleCardsPosition(oriTransX * offset, oriTransY * offset); 328 | dispatchOnDragEvent(card, false, false, oriTransX * offset, oriTransY * offset); 329 | 330 | } 331 | }); 332 | animator.addListener(new AnimatorListenerAdapter() { 333 | @Override 334 | public void onAnimationEnd(Animator animation) { 335 | ViewCompat.setTranslationZ(mRecyclerView, -1); 336 | } 337 | }); 338 | animator.setDuration(500); 339 | animator.start(); 340 | } 341 | 342 | // refresh other visible cards' positions when dragging 343 | private void refreshOtherVisibleCardsPosition(float offset_x, float offset_y) { 344 | float factor = (float) (Math.sqrt(offset_x * offset_x + offset_y * offset_y) 345 | / Math.sqrt(mDragThresholdX * mDragThresholdX + mDragThresholdY * mDragThresholdY)); 346 | factor = Math.min(factor, 1); 347 | if (getItemCount() > 1) { 348 | int cardOffset = mCardOffset; 349 | // count of all the attached cards 350 | int maxAttachChildrenCount = Math.min(mRecyclerView.getVisibleCardCount(), getItemCount() - 1); 351 | // count of all the cards required refreshing 352 | int totalRefreshingCount = Math.min(getItemCount() - 1, mRecyclerView.getVisibleCardCount()); 353 | for (int i = 1; i <= totalRefreshingCount; i++) { 354 | int childPosition = maxAttachChildrenCount - i; 355 | View child_i = getChildAt(childPosition); 356 | // transY 357 | if (i < mRecyclerView.getVisibleCardCount()) { 358 | child_i.setTranslationY(-2 * cardOffset * (float) Math.sqrt(factor)); 359 | } 360 | // scale 361 | float scaleX_start = (child_i.getWidth() - i * cardOffset * 2) * 1f / child_i.getWidth(); 362 | float scaleX_end = (child_i.getWidth() - (i - 1) * cardOffset * 2) * 1f / child_i.getWidth(); 363 | float scaleY_start = (child_i.getHeight() - i * cardOffset * 2) * 1f / child_i.getHeight(); 364 | float scaleY_end = (child_i.getHeight() - (i - 1) * cardOffset * 2) * 1f / child_i.getHeight(); 365 | float currentScaleX = scaleX_start + (scaleX_end - scaleX_start) * (float) Math.sqrt(factor); 366 | float currentScaleY = scaleY_start + (scaleY_end - scaleY_start) * (float) Math.sqrt(factor); 367 | child_i.setScaleX(currentScaleX); 368 | child_i.setScaleY(currentScaleY); 369 | 370 | // calculate the current card ori elevation 371 | int current = mRecyclerView.getVisibleCardCount() - i; 372 | int oriElevation = mCardElevation * current; 373 | int currentElevation = (int) (oriElevation + mCardElevation * (float) Math.sqrt(factor)); 374 | // update transZ 375 | ViewCompat.setTranslationZ(child_i, currentElevation); 376 | } 377 | } 378 | } 379 | 380 | private void dispatchOnDragEvent(View view, boolean isDragging, boolean isDropped, 381 | float offsetX, float offsetY) { 382 | if (null != mListener) { 383 | mListener.onDraggingStateChanged(view, isDragging, isDropped, offsetX, offsetY); 384 | if (isDragging) 385 | mListener.onCardDragging(view, offsetX, offsetY); 386 | } 387 | } 388 | } 389 | -------------------------------------------------------------------------------- /cardstackview/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | CardStackView 3 | 4 | -------------------------------------------------------------------------------- /cardstackview/src/test/java/com/devilist/cardstackview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.devilist.cardstackview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /images/image.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devilist/RecyclerCardStackView/45a4238c8705d1de3293e631918fe68a1b1467ef/images/image.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':cardstackview' 2 | --------------------------------------------------------------------------------