├── .gitattributes ├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── LICENSE.md ├── README.md ├── RecyclerViewSlideItem.iml ├── Swipeable-RecyclerView.iml ├── art └── demo.gif ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── library.iml ├── local.properties ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── tr4android │ │ └── recyclerviewslideitem │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── tr4android │ │ └── recyclerviewslideitem │ │ ├── SwipeAdapter.java │ │ ├── SwipeConfiguration.java │ │ ├── SwipeItem.java │ │ └── SwipeRunnable.java │ └── res │ ├── layout │ ├── item_swipe.xml │ ├── item_swipe_info.xml │ └── item_swipe_undo.xml │ ├── values-v21 │ └── styles.xml │ └── values │ ├── strings.xml │ └── styles.xml ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── sample.iml └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── tr4android │ │ └── recyclerviewslideitemsample │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── tr4android │ │ └── recyclerviewslideitemsample │ │ ├── DividerItemDecoration.java │ │ ├── SampleActivity.java │ │ └── SampleAdapter.java │ └── res │ ├── drawable-hdpi │ ├── ic_delete_white_24dp.png │ └── ic_done_white_24dp.png │ ├── drawable-mdpi │ ├── ic_delete_white_24dp.png │ └── ic_done_white_24dp.png │ ├── drawable-xhdpi │ ├── ic_delete_white_24dp.png │ └── ic_done_white_24dp.png │ ├── drawable-xxhdpi │ ├── ic_delete_white_24dp.png │ └── ic_done_white_24dp.png │ ├── drawable-xxxhdpi │ ├── ic_delete_white_24dp.png │ └── ic_done_white_24dp.png │ ├── drawable │ └── circle_grey.xml │ ├── layout │ ├── activity_sample.xml │ └── item_sample.xml │ ├── menu │ └── menu_sample.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .idea 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Swipeable-RecyclerView -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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 | #### :exclamation: IMPORTANT ANNOUNCEMENT: LIBRARY NOT MAINTAINED :exclamation: 2 | 3 | This library is no longer actively maintained, the last commit on it was on *Feb 2, 2016*. This is mainly due to the official APIs for `RecyclerView` added in versions 24.1.0 which supersede most of the implementations in this library. You can check out the Medium posts by @iPaulPro ([Part 1](https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf) and [Part 2](https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-6a6f0c422efd)) for an introduction to those, including some customization options. It is highly recommended to switch to those official APIs, but if you still want to contribute to this project I'll accept pull requests. Happy coding! 4 | 5 | --- 6 | 7 | [![Release](https://img.shields.io/github/release/TR4Android/Swipeable-RecyclerView.svg?label=JitPack%20Maven)](https://jitpack.io/#TR4Android/Swipeable-RecyclerView) [![API](https://img.shields.io/badge/API-7%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=7) [![License](https://img.shields.io/badge/license-Apache 2.0-brightgreen.svg?style=flat)](https://github.com/TR4Android/Swipeable-RecyclerView/blob/master/LICENSE.md) 8 | 9 | # Swipeable-RecyclerView 10 | A library that provides an easy and customizable way to implement a swipe to dismiss pattern with RecyclerView and works back to API level 7. 11 | 12 | **Note:** This library is currently in active development and might thus not be suitable for production versions as of yet. If you are comfortable experimenting with this library though feel free to give it a spin and report any issues you find. A list of issues currently on the roadmap can be found [here](https://github.com/TR4Android/Swipeable-RecyclerView/issues). 13 | 14 | Here's how the demo application looks and behaves (you can download a debug apk of the demo [here](https://github.com/TR4Android/Swipeable-RecyclerView/releases/download/0.2.0/sample-debug.apk)): 15 | 16 | ![Demo GIF](https://raw.githubusercontent.com/TR4Android/Swipeable-RecyclerView/master/art/demo.gif) 17 | 18 | ## Usage 19 | ### Importing the library 20 | 21 | This library is available as a gradle dependency via [JitPack.io](https://github.com/jitpack/jitpack.io). Just add the following lines to your app module `build.gradle`: 22 | ``` gradle 23 | repositories { 24 | maven { url "https://jitpack.io" } 25 | } 26 | dependencies { 27 | compile 'com.github.TR4Android:Swipeable-RecyclerView:0.2.0' 28 | } 29 | ``` 30 | 31 | ### Code Setup 32 | 33 | For a full example check out the implementation of the [SampleAdapter](https://github.com/TR4Android/Swipeable-RecyclerView/blob/master/sample/src/main/java/com/tr4android/recyclerviewslideitemsample/SampleAdapter.java) in the sample folder of this repository. 34 | 35 | To be able to use the swipe to dismiss pattern in your RecyclerView you'll have to extend the `SwipeAdapter` in your Adapter class. After that there are only a few minor changes you have to do to get everything going: 36 | 37 | ##### Migrating from normal adapter 38 | Override `onCreateSwipeViewHolder(ViewGroup parent, int viewType)` and `onBindSwipeViewHolder(ViewHolder holder, int position)` instead of the usual `onCreateViewHolder(ViewGroup parent, int viewType)` and `onBindViewHolder(ViewHolder holder, int position)`. This is needed to wrap your list item in a ViewGroup that handles swiping (namely `SwipeItem`) and handle its configuration. In additon to that you'll also have to replace the boolean `attachToRoot` with `true` so your list item gets attached to the wrapping SwipeItem. 39 | 40 | A full implementation might look something like this: 41 | ``` java 42 | public class SampleAdapter extends SwipeAdapter { 43 | ... 44 | @Override 45 | public RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int i) { 46 | View v = LayoutInflater.from(parent.getContext()) 47 | .inflate(R.layout.item_sample, parent, true); 48 | SampleViewHolder sampleViewHolder = new SampleViewHolder(v); 49 | return sampleViewHolder; 50 | } 51 | 52 | @Override 53 | public void onBindSwipeViewHolder(RecyclerView.ViewHolder holder, int position) { 54 | // handle data 55 | } 56 | ... 57 | } 58 | ``` 59 | 60 | ##### Special setup for swipeable adapter 61 | 62 | There also are some new methods related to the swiping pattern in the `SwipeAdapter` that you'll have to override. Those are: 63 | 64 | * `onCreateSwipeConfiguration(Context context, int position)`: This is used to determine the configuration of a particular list item and allows flexible control on a per item basis. You'll have to return a `SwipeConfiguration` using the built in `Builder` class. More customization options can be found in the SwipeConfiguration section below. 65 | * `onSwipe(int position, int direction)`: This gets called whenever an item is removed using a swipe. Be sure to call `notifyItemRemoved(position)` there after changing your data to properly allow removal using the default ItemAnimator of the RecyclerView. `int direction` is one of either `SWIPE_LEFT` or `SWIPE_RIGHT` indicating the direction in which the user has dismissed the item. 66 | 67 | An implementation might look like this: 68 | ``` java 69 | public class SampleAdapter extends SwipeAdapter { 70 | ... 71 | public SampleAdapter() { 72 | // retrieve your data 73 | } 74 | ... 75 | @Override 76 | public SwipeConfiguration onCreateSwipeConfiguration(Context context, int position) { 77 | return new SwipeConfiguration.Builder(context) 78 | .setBackgroundColorResource(R.color.color_delete) 79 | .setDrawableResource(R.drawable.ic_delete_white_24dp) 80 | .build(); 81 | } 82 | 83 | @Override 84 | public void onSwipe(int position, int direction) { 85 | mDataset.remove(position); 86 | notifyItemRemoved(position); 87 | } 88 | ... 89 | } 90 | ``` 91 | 92 | ##### Special setup for item layout 93 | 94 | There's practically no setup needed for the layout itself, but you'll probably want to add a background to the root of your item. If you just want to use the window background add `android:background="?android:attr/windowBackground"`. 95 | 96 | ### Customization 97 | 98 | You can easily customize the actions when swiping by using the `SwipeConfiguration` class which gives you full control over various aspects of this library. The following is a list of all currently available options. For all those there is also a corresponding `setLeft...()` and `setRight...()` flavor. 99 | 100 | * `setBackgroundColor(int color)`: The background color that appears behind the list item. 101 | * `setBackgroundColorResource(int resId)`: The resource id of the background color that appears behind the list item. 102 | * `setDrawableResource(int resId)`: The resource id of the drawable shown as a hint for the action. 103 | * `setDescriptionTextColor(int color)`: The text color used for the description and undo text. 104 | * `setDescriptionTextColorResource(int resId)`: The resource id of the text color used for the description and undo text. 105 | * `setDescription(CharSequence description)`: The text shown as a hint for the action. 106 | * `setDescriptionResource(int resId)`: The resource id of the text shown as a hint for the action. 107 | * `setUndoDescription(CharSequence description)`: The text shown when the user has dismissed the item and is shown the option to undo the dismissal. 108 | * `setUndoDescriptionResource(int resId)`: The resource id of the text shown when the user has dismissed the item and is shown the option to undo the dismissal. 109 | * `setUndoable(boolean undoable)`: Whether the action is undoable. If set to `true` the user will have the option to undo the action for 5 seconds, if set to `false` the item will be dismissed immediately. 110 | * `setSwipeBehaviour(SwipeBehavior swipeBehavior)`: The behaviour of the item when swiping. Takes one of the provided default behaviours `NORMAL_SWIPE`, `RESTRICTED_SWIPE` or `NO_SWIPE`. 111 | * `setSwipeBehaviour(float range, Interpolator interpolator)`: The more customized behaviour of the item when swiping, where `range` indicates how far the item can be swiped (percentage of item width) and `interpolator` is the custom Interpolator used when calculating the item position while swiping. 112 | * `setCallbackEnabled(boolean enabled)`: Whether the swipe callback should be triggered on this action. If set to `true` you will receive a swipe action through `onSwipe(int position, int direction)`, if set to `false` you won't. 113 | 114 | ## License 115 | 116 | Copyright 2015 Thomas Robert Altstidl 117 | 118 | Licensed under the Apache License, Version 2.0 (the "License"); 119 | you may not use this file except in compliance with the License. 120 | You may obtain a copy of the License at 121 | 122 | http://www.apache.org/licenses/LICENSE-2.0 123 | 124 | Unless required by applicable law or agreed to in writing, software 125 | distributed under the License is distributed on an "AS IS" BASIS, 126 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 127 | See the License for the specific language governing permissions and 128 | limitations under the License. 129 | 130 | ## License of RecyclerView and AppCompat v4 131 | 132 | Copyright 2015 The Android Open Source Project 133 | 134 | Licensed under the Apache License, Version 2.0 (the "License"); 135 | you may not use this file except in compliance with the License. 136 | You may obtain a copy of the License at 137 | 138 | http://www.apache.org/licenses/LICENSE-2.0 139 | 140 | Unless required by applicable law or agreed to in writing, software 141 | distributed under the License is distributed on an "AS IS" BASIS, 142 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 143 | See the License for the specific language governing permissions and 144 | limitations under the License. 145 | -------------------------------------------------------------------------------- /RecyclerViewSlideItem.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Swipeable-RecyclerView.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /art/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taltstidl/Swipeable-RecyclerView/65e1b066bd64837eadd260b5afa36ee841d28069/art/demo.gif -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.1.0' 9 | classpath 'com.github.dcendents:android-maven-plugin:1.2' 10 | 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /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 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taltstidl/Swipeable-RecyclerView/65e1b066bd64837eadd260b5afa36ee841d28069/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 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-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 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 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'android-maven' 3 | 4 | android { 5 | compileSdkVersion 23 6 | buildToolsVersion "23.0.2" 7 | 8 | defaultConfig { 9 | minSdkVersion 7 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:support-v4:23.1.1' 25 | compile 'com.android.support:recyclerview-v7:23.1.1' 26 | } 27 | -------------------------------------------------------------------------------- /library/library.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /library/local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | #Wed May 06 12:05:56 CEST 2015 11 | sdk.dir=C\:\\Users\\ThomasR\\AppData\\Local\\Android\\sdk 12 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\ThomasR\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/tr4android/recyclerviewslideitem/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.tr4android.recyclerviewslideitem; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/java/com/tr4android/recyclerviewslideitem/SwipeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Thomas Robert Altstidl 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 | package com.tr4android.recyclerviewslideitem; 18 | 19 | import android.content.Context; 20 | import android.os.Handler; 21 | import android.support.v7.widget.RecyclerView; 22 | import android.view.LayoutInflater; 23 | import android.view.ViewGroup; 24 | 25 | import java.util.ArrayList; 26 | 27 | public abstract class SwipeAdapter extends RecyclerView.Adapter { 28 | public static final int SWIPE_LEFT = -1; 29 | public static final int SWIPE_RIGHT = 1; 30 | public static final int TIME_POST_DELAYED = 5000; // in ms 31 | 32 | private Handler mHandler = new Handler(); 33 | private final ArrayList mRunnables = new ArrayList<>(); 34 | 35 | @Override 36 | public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 37 | SwipeItem item = (SwipeItem) LayoutInflater.from(parent.getContext()) 38 | .inflate(R.layout.item_swipe, parent, false); 39 | return onCreateSwipeViewHolder(item, viewType); 40 | } 41 | 42 | @Override 43 | public final void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { 44 | final RecyclerView.ViewHolder swipeHolder = holder; 45 | final SwipeItem swipeItem = (SwipeItem) swipeHolder.itemView; 46 | SwipeConfiguration configuration = onCreateSwipeConfiguration(swipeItem.getContext(), position); 47 | swipeItem.setSwipeConfiguration(configuration); 48 | swipeItem.setSwipeListener(new SwipeItem.OnSwipeListener() { 49 | @Override 50 | public void onSwipeLeft() { 51 | onSwipe(swipeHolder.getAdapterPosition(), SWIPE_LEFT); 52 | } 53 | 54 | @Override 55 | public void onSwipeRight() { 56 | onSwipe(swipeHolder.getAdapterPosition(), SWIPE_RIGHT); 57 | } 58 | 59 | @Override 60 | public void onSwipeLeftUndoStarted() { 61 | final int position = swipeHolder.getAdapterPosition(); 62 | if (position != RecyclerView.NO_POSITION) { 63 | SwipeRunnable runnable = new SwipeRunnable(SWIPE_LEFT) { 64 | @Override 65 | public void run() { 66 | synchronized (mRunnables) { 67 | onSwipe(mRunnables.indexOf(this), SWIPE_LEFT); 68 | } 69 | } 70 | }; 71 | synchronized (mRunnables) { 72 | mRunnables.set(position, runnable); 73 | mHandler.postDelayed(runnable, TIME_POST_DELAYED); 74 | } 75 | } 76 | } 77 | 78 | @Override 79 | public void onSwipeRightUndoStarted() { 80 | final int position = swipeHolder.getAdapterPosition(); 81 | if (position != RecyclerView.NO_POSITION) { 82 | SwipeRunnable runnable = new SwipeRunnable(SWIPE_RIGHT) { 83 | @Override 84 | public void run() { 85 | synchronized (mRunnables) { 86 | onSwipe(mRunnables.indexOf(this), SWIPE_RIGHT); 87 | } 88 | } 89 | }; 90 | synchronized (mRunnables) { 91 | mRunnables.set(position, runnable); 92 | mHandler.postDelayed(runnable, TIME_POST_DELAYED); 93 | } 94 | } 95 | } 96 | 97 | @Override 98 | public void onSwipeLeftUndoClicked() { 99 | final int position = swipeHolder.getAdapterPosition(); 100 | if (position != RecyclerView.NO_POSITION) { 101 | synchronized (mRunnables) { 102 | Runnable runnable = mRunnables.set(position, null); 103 | mHandler.removeCallbacks(runnable); 104 | } 105 | } 106 | } 107 | 108 | @Override 109 | public void onSwipeRightUndoClicked() { 110 | final int position = swipeHolder.getAdapterPosition(); 111 | if (position != RecyclerView.NO_POSITION) { 112 | synchronized (mRunnables) { 113 | Runnable runnable = mRunnables.set(position, null); 114 | mHandler.removeCallbacks(runnable); 115 | } 116 | } 117 | } 118 | }); 119 | // restore swipe state 120 | SwipeRunnable swipeRunnable = mRunnables.get(position); 121 | if (swipeRunnable != null) { 122 | swipeItem.setSwipeState(swipeRunnable.getDirection() == SWIPE_LEFT ? SwipeItem.SwipeState.LEFT_UNDO : SwipeItem.SwipeState.RIGHT_UNDO); 123 | } else { 124 | swipeItem.setSwipeState(SwipeItem.SwipeState.NORMAL); 125 | } 126 | onBindSwipeViewHolder(holder, position); 127 | } 128 | 129 | @Override 130 | public void onAttachedToRecyclerView(RecyclerView recyclerView) { 131 | if (mRunnables.size() == 0) { 132 | int count = getItemCount(); 133 | for (int i = 0; i < count; i++) { 134 | mRunnables.add(null); 135 | } 136 | } 137 | registerAdapterDataObserver(new SwipeAdapterDataObserver()); 138 | } 139 | 140 | public abstract RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int viewType); 141 | 142 | public abstract void onBindSwipeViewHolder(RecyclerView.ViewHolder holder, final int position); 143 | 144 | @Override 145 | public abstract int getItemCount(); 146 | 147 | public abstract SwipeConfiguration onCreateSwipeConfiguration(Context context, int position); 148 | 149 | public abstract void onSwipe(int position, int direction); 150 | 151 | /** 152 | * Observer for synchronized updating of undo runnables 153 | */ 154 | private class SwipeAdapterDataObserver extends RecyclerView.AdapterDataObserver { 155 | public void onChanged() { 156 | synchronized (mRunnables) { 157 | int size = mRunnables.size(); 158 | int itemCount = getItemCount(); 159 | if (itemCount > size) { 160 | onItemRangeChanged(0, size); 161 | onItemRangeInserted(size, itemCount - size); 162 | } else { 163 | onItemRangeChanged(0, itemCount); 164 | onItemRangeRemoved(itemCount, size - itemCount); 165 | } 166 | } 167 | } 168 | 169 | public void onItemRangeChanged(int positionStart, int itemCount) { 170 | synchronized (mRunnables) { 171 | for (int i = 0; i < itemCount; i++) { 172 | Runnable r = mRunnables.set(positionStart + i, null); 173 | if (r != null) { 174 | mHandler.removeCallbacks(r); 175 | } 176 | } 177 | } 178 | } 179 | 180 | public void onItemRangeInserted(int positionStart, int itemCount) { 181 | synchronized (mRunnables) { 182 | for (int i = 0; i < itemCount; i++) { 183 | mRunnables.add(positionStart, null); 184 | } 185 | } 186 | } 187 | 188 | public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { 189 | synchronized (mRunnables) { 190 | for (int i = 0; i < itemCount; i++) { 191 | int c = fromPosition > toPosition ? i : 0; 192 | mRunnables.set(toPosition + c, mRunnables.remove(fromPosition + c)); 193 | } 194 | } 195 | } 196 | 197 | public void onItemRangeRemoved(int positionStart, int itemCount) { 198 | synchronized (mRunnables) { 199 | for (int i = 0; i < itemCount; i++) { 200 | Runnable r = mRunnables.remove(positionStart); 201 | if (r != null) { 202 | mHandler.removeCallbacks(r); 203 | } 204 | } 205 | } 206 | } 207 | } 208 | } -------------------------------------------------------------------------------- /library/src/main/java/com/tr4android/recyclerviewslideitem/SwipeConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Thomas Robert Altstidl 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 | package com.tr4android.recyclerviewslideitem; 18 | 19 | import android.content.Context; 20 | import android.support.annotation.ColorRes; 21 | import android.support.annotation.DrawableRes; 22 | import android.support.annotation.StringRes; 23 | import android.view.animation.DecelerateInterpolator; 24 | import android.view.animation.Interpolator; 25 | import android.view.animation.LinearInterpolator; 26 | 27 | public class SwipeConfiguration { 28 | // drawable resId 29 | private int mLeftDrawableResource = 0; 30 | private int mRightDrawableResource = 0; 31 | // description text 32 | private CharSequence mLeftDescription; 33 | private CharSequence mRightDescription; 34 | // description text color 35 | private int mLeftDescriptionTextColor; 36 | private int mRightDescriptionTextColor; 37 | // background color 38 | private int mLeftBackgroundColor; 39 | private int mRightBackgroundColor; 40 | // whether callback is enabled when swiping 41 | private boolean mLeftCallbackEnabled = true; 42 | private boolean mRightCallbackEnabled = true; 43 | // whether action after swiping is undoable 44 | private boolean mLeftUndoable; 45 | private boolean mRightUndoable; 46 | // undo description text 47 | private CharSequence mLeftUndoDescription; 48 | private CharSequence mRightUndoDescription; 49 | // undo button text 50 | private CharSequence mLeftUndoButtonText; 51 | private CharSequence mRightUndoButtonText; 52 | // swipe range (value between 0 and 1.0) 53 | private float mLeftSwipeRange; 54 | private float mRightSwipeRange; 55 | // interpolator used for swipe animation 56 | private Interpolator mLeftSwipeInterpolator; 57 | private Interpolator mRightSwipeInterpolator; 58 | 59 | int getLeftDrawableResource() { 60 | return mLeftDrawableResource; 61 | } 62 | 63 | void setLeftDrawableResource(int resId) { 64 | mLeftDrawableResource = resId; 65 | } 66 | 67 | int getRightDrawableResource() { 68 | return mRightDrawableResource; 69 | } 70 | 71 | void setRightDrawableResource(int resId) { 72 | mRightDrawableResource = resId; 73 | } 74 | 75 | CharSequence getLeftDescription() { 76 | return mLeftDescription; 77 | } 78 | 79 | void setLeftDescription(CharSequence description) { 80 | mLeftDescription = description; 81 | } 82 | 83 | CharSequence getRightDescription() { 84 | return mRightDescription; 85 | } 86 | 87 | void setRightDescription(CharSequence description) { 88 | mRightDescription = description; 89 | } 90 | 91 | int getLeftDescriptionTextColor() { 92 | return mLeftDescriptionTextColor; 93 | } 94 | 95 | void setLeftDescriptionTextColor(int color) { 96 | mLeftDescriptionTextColor = color; 97 | } 98 | 99 | int getRightDescriptionTextColor() { 100 | return mRightDescriptionTextColor; 101 | } 102 | 103 | void setRightDescriptionTextColor(int color) { 104 | mRightDescriptionTextColor = color; 105 | } 106 | 107 | int getLeftBackgroundColor() { 108 | return mLeftBackgroundColor; 109 | } 110 | 111 | void setLeftBackgroundColor(int color) { 112 | mLeftBackgroundColor = color; 113 | } 114 | 115 | int getRightBackgroundColor() { 116 | return mRightBackgroundColor; 117 | } 118 | 119 | void setRightBackgroundColor(int color) { 120 | mRightBackgroundColor = color; 121 | } 122 | 123 | boolean isLeftCallbackEnabled() { 124 | return mLeftCallbackEnabled; 125 | } 126 | 127 | void setLeftCallbackEnabled(boolean enabled) { 128 | mLeftCallbackEnabled = enabled; 129 | } 130 | 131 | boolean isRightCallbackEnabled() { 132 | return mRightCallbackEnabled; 133 | } 134 | 135 | void setRightCallbackEnabled(boolean enabled) { 136 | mRightCallbackEnabled = enabled; 137 | } 138 | 139 | boolean isLeftUndoable() { 140 | return mLeftUndoable; 141 | } 142 | 143 | void setLeftUndoable(boolean undoable) { 144 | mLeftUndoable = undoable; 145 | } 146 | 147 | boolean isRightUndoable() { 148 | return mRightUndoable; 149 | } 150 | 151 | void setRightUndoable(boolean undoable) { 152 | mRightUndoable = undoable; 153 | } 154 | 155 | CharSequence getLeftUndoDescription() { 156 | return mLeftUndoDescription; 157 | } 158 | 159 | void setLeftUndoDescription(CharSequence description) { 160 | mLeftUndoDescription = description; 161 | } 162 | 163 | CharSequence getRightUndoDescription() { 164 | return mRightUndoDescription; 165 | } 166 | 167 | void setRightUndoDescription(CharSequence description) { 168 | mRightUndoDescription = description; 169 | } 170 | 171 | CharSequence getLeftUndoButtonText() { 172 | return mLeftUndoButtonText; 173 | } 174 | 175 | void setLeftUndoButtonText(CharSequence text) { 176 | mLeftUndoButtonText = text; 177 | } 178 | 179 | CharSequence getRightUndoButtonText() { 180 | return mRightUndoButtonText; 181 | } 182 | 183 | void setRightUndoButtonText(CharSequence text) { 184 | mRightUndoButtonText = text; 185 | } 186 | 187 | float getLeftSwipeRange() { 188 | return mLeftSwipeRange; 189 | } 190 | 191 | Interpolator getLeftSwipeInterpolator() { 192 | return mLeftSwipeInterpolator; 193 | } 194 | 195 | void setLeftSwipeBehaviour(float range, Interpolator interpolator) { 196 | mLeftSwipeRange = range; 197 | mLeftSwipeInterpolator = interpolator; 198 | } 199 | 200 | float getRightSwipeRange() { 201 | return mRightSwipeRange; 202 | } 203 | 204 | Interpolator getRightSwipeInterpolator() { 205 | return mRightSwipeInterpolator; 206 | } 207 | 208 | void setRightSwipeBehaviour(float range, Interpolator interpolator) { 209 | mRightSwipeRange = range; 210 | mRightSwipeInterpolator = interpolator; 211 | } 212 | 213 | /** 214 | * Class for default swipe behaviour provided by the library 215 | */ 216 | public static class SwipeBehaviour { 217 | // default interpolators 218 | private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator(); 219 | private static final Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator(); 220 | 221 | // default SwipeBehaviour values 222 | public static final SwipeBehaviour NORMAL_SWIPE = new SwipeBehaviour(1.0f, LINEAR_INTERPOLATOR); 223 | public static final SwipeBehaviour RESTRICTED_SWIPE = new SwipeBehaviour(0.25f, DECELERATE_INTERPOLATOR); 224 | public static final SwipeBehaviour NO_SWIPE = new SwipeBehaviour(0.0f, LINEAR_INTERPOLATOR); 225 | 226 | public float range; 227 | public Interpolator interpolator; 228 | 229 | SwipeBehaviour(float range, Interpolator interpolator) { 230 | this.range = range; 231 | this.interpolator = interpolator; 232 | } 233 | } 234 | 235 | /** 236 | * Builder class for building SwipeConfiguration 237 | */ 238 | public static class Builder { 239 | private Context mContext; 240 | private int mLeftDrawableResource = 0; 241 | private int mRightDrawableResource = 0; 242 | private CharSequence mLeftDescription; 243 | private CharSequence mRightDescription; 244 | private int mLeftDescriptionTextColor; 245 | private int mRightDescriptionTextColor; 246 | private int mLeftBackgroundColor; 247 | private int mRightBackgroundColor; 248 | private boolean mLeftCallbackEnabled = true; 249 | private boolean mRightCallbackEnabled = true; 250 | private boolean mLeftUndoable; 251 | private boolean mRightUndoable; 252 | private CharSequence mLeftUndoDescription; 253 | private CharSequence mRightUndoDescription; 254 | private CharSequence mLeftUndoButtonText; 255 | private CharSequence mRightUndoButtonText; 256 | private float mLeftSwipeRange; 257 | private float mRightSwipeRange; 258 | private Interpolator mLeftSwipeInterpolator; 259 | private Interpolator mRightSwipeInterpolator; 260 | 261 | public Builder(Context context) { 262 | mContext = context; 263 | } 264 | 265 | public Builder setLeftDrawableResource(@DrawableRes int resId) { 266 | mLeftDrawableResource = resId; 267 | return this; 268 | } 269 | 270 | public Builder setRightDrawableResource(@DrawableRes int resId) { 271 | mRightDrawableResource = resId; 272 | return this; 273 | } 274 | 275 | public Builder setDrawableResource(@DrawableRes int resId) { 276 | mLeftDrawableResource = resId; 277 | mRightDrawableResource = resId; 278 | return this; 279 | } 280 | 281 | public Builder setLeftDescription(CharSequence description) { 282 | mLeftDescription = description; 283 | return this; 284 | } 285 | 286 | public Builder setLeftDescription(@StringRes int resId) { 287 | mLeftDescription = mContext.getString(resId); 288 | return this; 289 | } 290 | 291 | public Builder setRightDescription(CharSequence description) { 292 | mRightDescription = description; 293 | return this; 294 | } 295 | 296 | public Builder setRightDescription(@StringRes int resId) { 297 | mRightDescription = mContext.getString(resId); 298 | return this; 299 | } 300 | 301 | public Builder setDescription(CharSequence description) { 302 | mLeftDescription = description; 303 | mRightDescription = description; 304 | return this; 305 | } 306 | 307 | public Builder setDescription(@StringRes int resId) { 308 | mLeftDescription = mContext.getString(resId); 309 | mRightDescription = mContext.getString(resId); 310 | return this; 311 | } 312 | 313 | public Builder setLeftDescriptionTextColor(int color) { 314 | mLeftDescriptionTextColor = color; 315 | return this; 316 | } 317 | 318 | public Builder setLeftDescriptionTextColorResource(@ColorRes int resId) { 319 | mLeftDescriptionTextColor = mContext.getResources().getColor(resId); 320 | return this; 321 | } 322 | 323 | public Builder setRightDescriptionTextColor(int color) { 324 | mRightDescriptionTextColor = color; 325 | return this; 326 | } 327 | 328 | public Builder setRightDescriptionTextColorResource(@ColorRes int resId) { 329 | mRightDescriptionTextColor = mContext.getResources().getColor(resId); 330 | return this; 331 | } 332 | 333 | public Builder setDescriptionTextColor(int color) { 334 | mLeftDescriptionTextColor = color; 335 | mRightDescriptionTextColor = color; 336 | return this; 337 | } 338 | 339 | public Builder setDescriptionTextColorResource(@ColorRes int resId) { 340 | mLeftDescriptionTextColor = mContext.getResources().getColor(resId); 341 | mRightDescriptionTextColor = mContext.getResources().getColor(resId); 342 | return this; 343 | } 344 | 345 | public Builder setLeftBackgroundColor(int color) { 346 | mLeftBackgroundColor = color; 347 | return this; 348 | } 349 | 350 | public Builder setLeftBackgroundColorResource(@ColorRes int resId) { 351 | mLeftBackgroundColor = mContext.getResources().getColor(resId); 352 | return this; 353 | } 354 | 355 | public Builder setRightBackgroundColor(int color) { 356 | mRightBackgroundColor = color; 357 | return this; 358 | } 359 | 360 | public Builder setRightBackgroundColorResource(@ColorRes int resId) { 361 | mRightBackgroundColor = mContext.getResources().getColor(resId); 362 | return this; 363 | } 364 | 365 | public Builder setBackgroundColor(int color) { 366 | mLeftBackgroundColor = color; 367 | mRightBackgroundColor = color; 368 | return this; 369 | } 370 | 371 | public Builder setBackgroundColorResource(@ColorRes int resId) { 372 | mLeftBackgroundColor = mContext.getResources().getColor(resId); 373 | mRightBackgroundColor = mContext.getResources().getColor(resId); 374 | return this; 375 | } 376 | 377 | public Builder setLeftCallbackEnabled(boolean enabled) { 378 | mLeftCallbackEnabled = enabled; 379 | return this; 380 | } 381 | 382 | public Builder setRightCallbackEnabled(boolean enabled) { 383 | mRightCallbackEnabled = enabled; 384 | return this; 385 | } 386 | 387 | public Builder setCallbackEnabled(boolean enabled) { 388 | mLeftCallbackEnabled = enabled; 389 | mRightCallbackEnabled = enabled; 390 | return this; 391 | } 392 | 393 | public Builder setLeftUndoable(boolean undoable) { 394 | mLeftUndoable = undoable; 395 | return this; 396 | } 397 | 398 | public Builder setRightUndoable(boolean undoable) { 399 | mRightUndoable = undoable; 400 | return this; 401 | } 402 | 403 | public Builder setUndoable(boolean undoable) { 404 | mLeftUndoable = undoable; 405 | mRightUndoable = undoable; 406 | return this; 407 | } 408 | 409 | public Builder setLeftUndoDescription(CharSequence description) { 410 | mLeftUndoDescription = description; 411 | return this; 412 | } 413 | 414 | public Builder setLeftUndoDescription(@StringRes int resid) { 415 | mLeftUndoDescription = mContext.getString(resid); 416 | return this; 417 | } 418 | 419 | public Builder setRightUndoDescription(CharSequence description) { 420 | mRightUndoDescription = description; 421 | return this; 422 | } 423 | 424 | public Builder setRightUndoDescription(@StringRes int resId) { 425 | mRightUndoDescription = mContext.getString(resId); 426 | return this; 427 | } 428 | 429 | public Builder setUndoDescription(CharSequence description) { 430 | mLeftUndoDescription = description; 431 | mRightUndoDescription = description; 432 | return this; 433 | } 434 | 435 | public Builder setUndoDescription(@StringRes int resId) { 436 | mLeftUndoDescription = mContext.getString(resId); 437 | mRightUndoDescription = mContext.getString(resId); 438 | return this; 439 | } 440 | 441 | public Builder setLeftUndoButtonText(CharSequence text) { 442 | mLeftUndoButtonText = text; 443 | return this; 444 | } 445 | 446 | public Builder setLeftUndoButtonText(@StringRes int resId) { 447 | mLeftUndoButtonText = mContext.getString(resId); 448 | return this; 449 | } 450 | 451 | public Builder setRightUndoButtonText(CharSequence text) { 452 | mRightUndoButtonText = text; 453 | return this; 454 | } 455 | 456 | public Builder setRightUndoButtonText(@StringRes int resId) { 457 | mRightUndoButtonText = mContext.getString(resId); 458 | return this; 459 | } 460 | 461 | public Builder setUndoButtonText(CharSequence text) { 462 | mLeftUndoButtonText = text; 463 | mRightUndoButtonText = text; 464 | return this; 465 | } 466 | 467 | public Builder setUndoButtonText(@StringRes int resId) { 468 | mLeftUndoButtonText = mContext.getString(resId); 469 | mRightUndoButtonText = mContext.getString(resId); 470 | return this; 471 | } 472 | 473 | public Builder setLeftSwipeBehaviour(float range, Interpolator interpolator) { 474 | mLeftSwipeRange = range; 475 | mLeftSwipeInterpolator = interpolator; 476 | return this; 477 | } 478 | 479 | public Builder setLeftSwipeBehaviour(SwipeBehaviour swipeBehaviour) { 480 | mLeftSwipeRange = swipeBehaviour.range; 481 | mLeftSwipeInterpolator = swipeBehaviour.interpolator; 482 | return this; 483 | } 484 | 485 | public Builder setRightSwipeBehaviour(float range, Interpolator interpolator) { 486 | mRightSwipeRange = range; 487 | mRightSwipeInterpolator = interpolator; 488 | return this; 489 | } 490 | 491 | public Builder setRightSwipeBehaviour(SwipeBehaviour swipeBehaviour) { 492 | mRightSwipeRange = swipeBehaviour.range; 493 | mRightSwipeInterpolator = swipeBehaviour.interpolator; 494 | return this; 495 | } 496 | 497 | public Builder setSwipeBehaviour(float range, Interpolator interpolator) { 498 | mLeftSwipeRange = range; 499 | mLeftSwipeInterpolator = interpolator; 500 | mRightSwipeRange = range; 501 | mRightSwipeInterpolator = interpolator; 502 | return this; 503 | } 504 | 505 | public Builder setSwipeBehaviour(SwipeBehaviour swipeBehaviour) { 506 | mLeftSwipeRange = swipeBehaviour.range; 507 | mLeftSwipeInterpolator = swipeBehaviour.interpolator; 508 | mRightSwipeRange = swipeBehaviour.range; 509 | mRightSwipeInterpolator = swipeBehaviour.interpolator; 510 | return this; 511 | } 512 | 513 | public SwipeConfiguration build() { 514 | SwipeConfiguration config = new SwipeConfiguration(); 515 | config.setLeftDrawableResource(mLeftDrawableResource); 516 | config.setRightDrawableResource(mRightDrawableResource); 517 | config.setLeftDescription(mLeftDescription); 518 | config.setRightDescription(mRightDescription); 519 | config.setLeftDescriptionTextColor(mLeftDescriptionTextColor); 520 | config.setRightDescriptionTextColor(mRightDescriptionTextColor); 521 | config.setLeftBackgroundColor(mLeftBackgroundColor); 522 | config.setRightBackgroundColor(mRightBackgroundColor); 523 | config.setLeftCallbackEnabled(mLeftCallbackEnabled); 524 | config.setRightCallbackEnabled(mRightCallbackEnabled); 525 | config.setLeftUndoable(mLeftUndoable); 526 | config.setRightUndoable(mRightUndoable); 527 | config.setLeftUndoDescription(mLeftUndoDescription); 528 | config.setRightUndoDescription(mRightUndoDescription); 529 | config.setLeftUndoButtonText(mLeftUndoButtonText); 530 | config.setRightUndoButtonText(mRightUndoButtonText); 531 | if (mLeftSwipeInterpolator == null) { 532 | setLeftSwipeBehaviour(SwipeBehaviour.NORMAL_SWIPE); 533 | } 534 | config.setLeftSwipeBehaviour(mLeftSwipeRange, mLeftSwipeInterpolator); 535 | if (mRightSwipeInterpolator == null) { 536 | setRightSwipeBehaviour(SwipeBehaviour.NORMAL_SWIPE); 537 | } 538 | config.setRightSwipeBehaviour(mRightSwipeRange, mRightSwipeInterpolator); 539 | return config; 540 | } 541 | } 542 | } -------------------------------------------------------------------------------- /library/src/main/java/com/tr4android/recyclerviewslideitem/SwipeItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Thomas Robert Altstidl 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 | package com.tr4android.recyclerviewslideitem; 18 | 19 | import android.content.Context; 20 | import android.support.v4.view.MotionEventCompat; 21 | import android.support.v4.view.ViewCompat; 22 | import android.support.v4.view.ViewPropertyAnimatorCompat; 23 | import android.support.v4.view.ViewPropertyAnimatorListener; 24 | import android.support.v4.widget.ViewDragHelper; 25 | import android.util.AttributeSet; 26 | import android.view.MotionEvent; 27 | import android.view.View; 28 | import android.view.ViewConfiguration; 29 | import android.view.ViewGroup; 30 | import android.view.ViewParent; 31 | import android.widget.ImageView; 32 | import android.widget.TextView; 33 | 34 | public class SwipeItem extends ViewGroup { 35 | //private static final String LOG_TAG = "SwipeItem"; 36 | 37 | private final ViewDragHelper mDragHelper; 38 | 39 | private OnSwipeListener mOnSwipeListener; 40 | 41 | private int mHorizontalDragRange; 42 | 43 | // custom view that slides 44 | private View mSwipeItem; 45 | 46 | // included background view for slide hint 47 | private View mSwipeInfo; 48 | 49 | // included background view for slide undo 50 | private View mSwipeUndo; 51 | 52 | private SwipeConfiguration mConfiguration; 53 | 54 | private boolean mFirstLayout = true; 55 | 56 | private int mTouchSlop; 57 | 58 | private int mPreviousPosition = 0; 59 | 60 | private boolean mHasPassedLeftThreshold; 61 | 62 | private boolean mHasPassedRightThreshold; 63 | 64 | protected enum SwipeState { 65 | LEFT_UNDO, RIGHT_UNDO, NORMAL 66 | } 67 | 68 | private SwipeState mState; 69 | 70 | // click listeners for undo actions 71 | private final OnClickListener leftUndoClickListener = new OnClickListener() { 72 | @Override 73 | public void onClick(View v) { 74 | showUndoAction(false); 75 | mSwipeInfo.setOnClickListener(null); 76 | swipeBack(); 77 | mState = SwipeState.NORMAL; 78 | // let swipe adapter handle canceled swipe 79 | dispatchOnSwipeLeftUndoClicked(); 80 | } 81 | }; 82 | 83 | private final OnClickListener rightUndoClickListener = new OnClickListener() { 84 | @Override 85 | public void onClick(View v) { 86 | showUndoAction(false); 87 | mSwipeInfo.setOnClickListener(null); 88 | swipeBack(); 89 | mState = SwipeState.NORMAL; 90 | // let swipe adapter handle canceled swipe 91 | dispatchOnSwipeRightUndoClicked(); 92 | } 93 | }; 94 | 95 | public SwipeItem(Context context) { 96 | this(context, null); 97 | } 98 | 99 | public SwipeItem(Context context, AttributeSet attrs) { 100 | this(context, attrs, 0); 101 | } 102 | 103 | public SwipeItem(Context context, AttributeSet attrs, int defStyleAttr) { 104 | super(context, attrs, defStyleAttr); 105 | 106 | // scroll threshold 107 | ViewConfiguration vc = ViewConfiguration.get(this.getContext()); 108 | mTouchSlop = vc.getScaledTouchSlop(); 109 | 110 | mDragHelper = ViewDragHelper.create(this, new DragHelperCallback()); 111 | } 112 | 113 | @Override 114 | protected void onAttachedToWindow() { 115 | super.onAttachedToWindow(); 116 | mFirstLayout = true; 117 | mPreviousPosition = 0; 118 | } 119 | 120 | @Override 121 | protected void onDetachedFromWindow() { 122 | super.onDetachedFromWindow(); 123 | mFirstLayout = true; 124 | mPreviousPosition = 0; 125 | } 126 | 127 | @Override 128 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 129 | super.onSizeChanged(w, h, oldw, oldh); 130 | if (h != oldh) { 131 | mFirstLayout = true; 132 | mPreviousPosition = 0; 133 | } 134 | } 135 | 136 | @Override 137 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 138 | // measure childs 139 | mSwipeInfo = getChildAt(0); 140 | mSwipeUndo = getChildAt(1); 141 | mSwipeItem = getChildAt(2); 142 | if (mFirstLayout) { 143 | switch (mState) { 144 | case LEFT_UNDO: 145 | mSwipeInfo.setVisibility(GONE); 146 | mSwipeUndo.setVisibility(VISIBLE); 147 | break; 148 | case RIGHT_UNDO: 149 | mSwipeInfo.setVisibility(GONE); 150 | mSwipeUndo.setVisibility(VISIBLE); 151 | break; 152 | case NORMAL: 153 | mSwipeUndo.setVisibility(GONE); 154 | mSwipeInfo.setVisibility(VISIBLE); 155 | } 156 | ViewCompat.setAlpha(mSwipeInfo, 1); 157 | ViewCompat.setAlpha(mSwipeUndo, 1); 158 | } 159 | 160 | measureChildWithMargins(mSwipeInfo, widthMeasureSpec, 0, heightMeasureSpec, 0); 161 | measureChildWithMargins(mSwipeUndo, widthMeasureSpec, 0, heightMeasureSpec, 0); 162 | measureChildWithMargins(mSwipeItem, widthMeasureSpec, 0, heightMeasureSpec, 0); 163 | 164 | setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mSwipeItem.getMeasuredHeight()); 165 | 166 | mSwipeInfo.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); 167 | mSwipeUndo.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); 168 | mSwipeItem.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); 169 | } 170 | 171 | @Override 172 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 173 | final int parentLeft = getPaddingLeft(); 174 | final int parentRight = right - left - getPaddingRight(); 175 | final int parentTop = getPaddingTop(); 176 | 177 | if (mFirstLayout) { 178 | // restore state 179 | int childLeft = parentLeft; 180 | int childRight = parentRight; 181 | switch (mState) { 182 | case LEFT_UNDO: 183 | childLeft -= mHorizontalDragRange; 184 | childRight -= mHorizontalDragRange; 185 | break; 186 | case RIGHT_UNDO: 187 | childLeft += mHorizontalDragRange; 188 | childRight += mHorizontalDragRange; 189 | break; 190 | } 191 | mHorizontalDragRange = getMeasuredWidth(); 192 | mSwipeItem.layout(childLeft, parentTop, childRight, parentTop + mSwipeItem.getMeasuredHeight()); 193 | mFirstLayout = false; 194 | } 195 | 196 | if (mSwipeInfo.getVisibility() != GONE) 197 | mSwipeInfo.layout(parentLeft, parentTop, parentRight, parentTop + mSwipeItem.getMeasuredHeight()); 198 | if (mSwipeUndo.getVisibility() != GONE) 199 | mSwipeUndo.layout(parentLeft, parentTop, parentRight, parentTop + mSwipeItem.getMeasuredHeight()); 200 | } 201 | 202 | @Override 203 | public boolean onInterceptTouchEvent(MotionEvent ev) { 204 | return mDragHelper.shouldInterceptTouchEvent(ev); 205 | } 206 | 207 | @Override 208 | public boolean onTouchEvent(MotionEvent ev) { 209 | mDragHelper.processTouchEvent(ev); 210 | // handle parent scroll behaviour 211 | if (Math.abs(mSwipeItem.getLeft()) > mTouchSlop) { 212 | // disable parent scrolling 213 | ViewParent parent = getParent(); 214 | if (parent != null) parent.requestDisallowInterceptTouchEvent(true); 215 | } else if (MotionEventCompat.getActionMasked(ev) == MotionEvent.ACTION_UP || MotionEventCompat.getActionMasked(ev) == MotionEvent.ACTION_CANCEL) { 216 | // enable parent scrolling 217 | ViewParent parent = getParent(); 218 | if (parent != null) parent.requestDisallowInterceptTouchEvent(false); 219 | } 220 | return true; 221 | } 222 | 223 | @Override 224 | public void computeScroll() { 225 | if (mDragHelper != null && mDragHelper.continueSettling(true)) { 226 | ViewCompat.postInvalidateOnAnimation(this); 227 | } 228 | } 229 | 230 | public void setSwipeBackgroundColor(int resolvedColor) { 231 | setBackgroundColor(resolvedColor); 232 | } 233 | 234 | public void setSwipeLeftImageResource(int resId) { 235 | ((ImageView) findViewById(R.id.imageViewLeft)).setImageResource(0); 236 | ((ImageView) findViewById(R.id.imageViewRight)).setImageResource(resId); 237 | } 238 | 239 | public void setSwipeRightImageResource(int resId) { 240 | ((ImageView) findViewById(R.id.imageViewLeft)).setImageResource(resId); 241 | ((ImageView) findViewById(R.id.imageViewRight)).setImageResource(0); 242 | } 243 | 244 | public void setSwipeDescription(CharSequence description) { 245 | ((TextView) findViewById(R.id.textViewDescription)).setText(description); 246 | } 247 | 248 | public void setSwipeUndoDescription(CharSequence description) { 249 | ((TextView) findViewById(R.id.undoDescription)).setText(description); 250 | } 251 | 252 | public void setSwipeDescriptionTextColor(int resolvedTextColor) { 253 | ((TextView) findViewById(R.id.textViewDescription)).setTextColor(resolvedTextColor); 254 | ((TextView) findViewById(R.id.undoDescription)).setTextColor(resolvedTextColor); 255 | ((TextView) findViewById(R.id.undoButton)).setTextColor(resolvedTextColor); 256 | } 257 | 258 | public void setSwipeConfiguration(SwipeConfiguration configuration) { 259 | mConfiguration = configuration; 260 | // TODO: handle configuration here if equal in both directions 261 | } 262 | 263 | protected void setSwipeState(SwipeState state) { 264 | mState = state; 265 | switch (mState) { 266 | case LEFT_UNDO: 267 | setSwipeBackgroundColor(mConfiguration.getLeftBackgroundColor()); 268 | setSwipeUndoDescription(mConfiguration.getLeftUndoDescription()); 269 | setSwipeDescriptionTextColor(mConfiguration.getLeftDescriptionTextColor()); 270 | findViewById(R.id.undoButton).setOnClickListener(leftUndoClickListener); 271 | break; 272 | case RIGHT_UNDO: 273 | setSwipeBackgroundColor(mConfiguration.getRightBackgroundColor()); 274 | setSwipeUndoDescription(mConfiguration.getRightUndoDescription()); 275 | setSwipeDescriptionTextColor(mConfiguration.getRightDescriptionTextColor()); 276 | findViewById(R.id.undoButton).setOnClickListener(rightUndoClickListener); 277 | break; 278 | } 279 | } 280 | 281 | public void setSwipeListener(OnSwipeListener listener) { 282 | mOnSwipeListener = listener; 283 | } 284 | 285 | public interface OnSwipeListener { 286 | /** 287 | * Called when the SwipeItem was swiped away to the left 288 | */ 289 | void onSwipeLeft(); 290 | 291 | /** 292 | * Called when the SwipeItem was swiped away to the right 293 | */ 294 | void onSwipeRight(); 295 | 296 | /** 297 | * Called when the SwipeItem was swiped away to the left and an undo action is started 298 | */ 299 | void onSwipeLeftUndoStarted(); 300 | 301 | /** 302 | * Called when the SwipeItem was swiped away to the right and an undo action is started 303 | */ 304 | void onSwipeRightUndoStarted(); 305 | 306 | /** 307 | * Called when the SwipeItem was swiped away to the left and undo was clicked 308 | */ 309 | void onSwipeLeftUndoClicked(); 310 | 311 | /** 312 | * Called when the SwipeItem was swiped away to the right and undo was clicked 313 | */ 314 | void onSwipeRightUndoClicked(); 315 | } 316 | 317 | void dispatchOnSwipeLeft() { 318 | if (mOnSwipeListener != null && mConfiguration.isLeftCallbackEnabled()) { 319 | mOnSwipeListener.onSwipeLeft(); 320 | } 321 | } 322 | 323 | void dispatchOnSwipeRight() { 324 | if (mOnSwipeListener != null && mConfiguration.isRightCallbackEnabled()) { 325 | mOnSwipeListener.onSwipeRight(); 326 | } 327 | } 328 | 329 | void dispatchOnSwipeLeftUndoStarted() { 330 | if (mOnSwipeListener != null && mConfiguration.isLeftCallbackEnabled()) { 331 | mOnSwipeListener.onSwipeLeftUndoStarted(); 332 | } 333 | } 334 | 335 | void dispatchOnSwipeRightUndoStarted() { 336 | if (mOnSwipeListener != null && mConfiguration.isRightCallbackEnabled()) { 337 | mOnSwipeListener.onSwipeRightUndoStarted(); 338 | } 339 | } 340 | 341 | void dispatchOnSwipeLeftUndoClicked() { 342 | if (mOnSwipeListener != null && mConfiguration.isLeftCallbackEnabled()) { 343 | mOnSwipeListener.onSwipeLeftUndoClicked(); 344 | } 345 | } 346 | 347 | void dispatchOnSwipeRightUndoClicked() { 348 | if (mOnSwipeListener != null && mConfiguration.isRightCallbackEnabled()) { 349 | mOnSwipeListener.onSwipeRightUndoClicked(); 350 | } 351 | } 352 | 353 | private class DragHelperCallback extends ViewDragHelper.Callback { 354 | 355 | @Override 356 | public boolean tryCaptureView(View view, int i) { 357 | return view == mSwipeItem; 358 | } 359 | 360 | @Override 361 | public int clampViewPositionHorizontal(View child, int left, int dx) { 362 | if (left < 0) { 363 | return child.getLeft() + Math.round(mConfiguration.getLeftSwipeRange() * dx); 364 | } else { 365 | return child.getLeft() + Math.round(mConfiguration.getRightSwipeRange() * dx); 366 | } 367 | } 368 | 369 | @Override 370 | public int getViewHorizontalDragRange(View child) { 371 | return mHorizontalDragRange; 372 | } 373 | 374 | @Override 375 | public void onViewDragStateChanged(int state) { 376 | if (state == ViewDragHelper.STATE_IDLE) { 377 | if (mSwipeItem.getLeft() == -mHorizontalDragRange) { 378 | handleLeftSwipe(); 379 | } else if (mSwipeItem.getLeft() == mHorizontalDragRange) { 380 | handleRightSwipe(); 381 | } else if (mSwipeItem.getLeft() == 0) { 382 | // check whether settled from restricted swipe 383 | if (mConfiguration.getLeftSwipeRange() != 1.0f && mHasPassedLeftThreshold) { 384 | mHasPassedLeftThreshold = false; 385 | dispatchOnSwipeLeft(); 386 | } 387 | if (mConfiguration.getRightSwipeRange() != 1.0f && mHasPassedRightThreshold) { 388 | mHasPassedRightThreshold = false; 389 | dispatchOnSwipeRight(); 390 | } 391 | } 392 | } 393 | } 394 | 395 | @Override 396 | public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { 397 | handlePositionChange(left); 398 | } 399 | 400 | @Override 401 | public void onViewReleased(View releasedChild, float xvel, float yvel) { 402 | // logic for slide behaviour 403 | if (xvel < 0 && mConfiguration.getLeftSwipeRange() == 1.0f) { 404 | // dismiss to left 405 | mDragHelper.settleCapturedViewAt(-mHorizontalDragRange, releasedChild.getTop()); 406 | } else if (xvel > 0 && mConfiguration.getRightSwipeRange() == 1.0f) { 407 | // dismiss to right 408 | mDragHelper.settleCapturedViewAt(mHorizontalDragRange, releasedChild.getTop()); 409 | } else { 410 | // not enough velocity to dismiss 411 | mDragHelper.settleCapturedViewAt(0, releasedChild.getTop()); 412 | } 413 | 414 | invalidate(); 415 | } 416 | } 417 | 418 | private void handlePositionChange(int newLeft) { 419 | if (newLeft > 0) { 420 | if (mPreviousPosition <= 0) { 421 | // show right action 422 | setSwipeBackgroundColor(mConfiguration.getRightBackgroundColor()); 423 | setSwipeRightImageResource(mConfiguration.getRightDrawableResource()); 424 | setSwipeDescription(mConfiguration.getRightDescription()); 425 | setSwipeDescriptionTextColor(mConfiguration.getRightDescriptionTextColor()); 426 | } 427 | float rightRange = mConfiguration.getRightSwipeRange(); 428 | if (rightRange != 1.0f && newLeft > Math.round(mHorizontalDragRange * rightRange * 0.75f)) { 429 | mHasPassedRightThreshold = true; 430 | mHasPassedLeftThreshold = false; 431 | } 432 | } else if (newLeft < 0) { 433 | if (mPreviousPosition >= 0) { 434 | // show left action 435 | setSwipeBackgroundColor(mConfiguration.getLeftBackgroundColor()); 436 | setSwipeLeftImageResource(mConfiguration.getLeftDrawableResource()); 437 | setSwipeDescription(mConfiguration.getLeftDescription()); 438 | setSwipeDescriptionTextColor(mConfiguration.getLeftDescriptionTextColor()); 439 | } 440 | float leftRange = mConfiguration.getLeftSwipeRange(); 441 | if (leftRange != 1.0f && newLeft < (-mHorizontalDragRange * leftRange * 0.75f)) { 442 | mHasPassedLeftThreshold = true; 443 | mHasPassedRightThreshold = false; 444 | } 445 | } 446 | mPreviousPosition = newLeft; 447 | } 448 | 449 | private void handleLeftSwipe() { 450 | if (mConfiguration.isLeftUndoable()) { 451 | mState = SwipeState.LEFT_UNDO; 452 | setSwipeUndoDescription(mConfiguration.getLeftUndoDescription()); 453 | showUndoAction(true); 454 | mSwipeUndo.findViewById(R.id.undoButton).setOnClickListener(leftUndoClickListener); 455 | // let swipe adapter handle started swipe with undo action 456 | dispatchOnSwipeLeftUndoStarted(); 457 | } else { 458 | dispatchOnSwipeLeft(); 459 | } 460 | } 461 | 462 | private void handleRightSwipe() { 463 | if (mConfiguration.isRightUndoable()) { 464 | mState = SwipeState.RIGHT_UNDO; 465 | setSwipeUndoDescription(mConfiguration.getRightUndoDescription()); 466 | showUndoAction(true); 467 | mSwipeUndo.findViewById(R.id.undoButton).setOnClickListener(rightUndoClickListener); 468 | // let swipe adapter handle started swipe with undo action 469 | dispatchOnSwipeRightUndoStarted(); 470 | } else { 471 | dispatchOnSwipeRight(); 472 | } 473 | } 474 | 475 | private void swipeBack() { 476 | if (mDragHelper.smoothSlideViewTo(mSwipeItem, 0, mSwipeItem.getTop())) { 477 | ViewCompat.postInvalidateOnAnimation(this); 478 | } 479 | } 480 | 481 | private void showUndoAction(final boolean show) { 482 | ViewCompat.setAlpha(mSwipeUndo, show ? 0 : 1); 483 | ViewCompat.setAlpha(mSwipeInfo, show ? 1 : 0); 484 | if (show) mSwipeUndo.setVisibility(VISIBLE); 485 | if (!show) mSwipeInfo.setVisibility(VISIBLE); 486 | final ViewPropertyAnimatorCompat undoAnimation = ViewCompat.animate(mSwipeUndo); 487 | undoAnimation.setDuration(300) 488 | .alpha(show ? 1 : 0).setListener(new ViewPropertyAnimatorListener() { 489 | @Override 490 | public void onAnimationStart(View view) { 491 | } 492 | 493 | @Override 494 | public void onAnimationEnd(View view) { 495 | undoAnimation.setListener(null); 496 | ViewCompat.setAlpha(view, show ? 1 : 0); 497 | if (!show) view.setVisibility(GONE); 498 | } 499 | 500 | @Override 501 | public void onAnimationCancel(View view) { 502 | } 503 | }).start(); 504 | final ViewPropertyAnimatorCompat infoAnimation = ViewCompat.animate(mSwipeInfo); 505 | infoAnimation.setDuration(300) 506 | .alpha(show ? 0 : 1).setListener(new ViewPropertyAnimatorListener() { 507 | @Override 508 | public void onAnimationStart(View view) { 509 | } 510 | 511 | @Override 512 | public void onAnimationEnd(View view) { 513 | infoAnimation.setListener(null); 514 | ViewCompat.setAlpha(view, show ? 0 : 1); 515 | if (show) view.setVisibility(GONE); 516 | } 517 | 518 | @Override 519 | public void onAnimationCancel(View view) { 520 | } 521 | }).start(); 522 | } 523 | 524 | @Override 525 | public LayoutParams generateLayoutParams(AttributeSet attrs) { 526 | return new SwipeItem.LayoutParams(getContext(), attrs); 527 | } 528 | 529 | @Override 530 | protected LayoutParams generateDefaultLayoutParams() { 531 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 532 | } 533 | 534 | @Override 535 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 536 | return new LayoutParams(p); 537 | } 538 | 539 | @Override 540 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 541 | return p instanceof LayoutParams; 542 | } 543 | 544 | public static class LayoutParams extends MarginLayoutParams { 545 | 546 | public LayoutParams(Context c, AttributeSet attrs) { 547 | super(c, attrs); 548 | } 549 | 550 | public LayoutParams(int width, int height) { 551 | super(width, height); 552 | } 553 | 554 | public LayoutParams(int width, int height, int gravity) { 555 | super(width, height); 556 | } 557 | 558 | public LayoutParams(ViewGroup.LayoutParams source) { 559 | super(source); 560 | } 561 | 562 | public LayoutParams(ViewGroup.MarginLayoutParams source) { 563 | super(source); 564 | } 565 | 566 | public LayoutParams(LayoutParams source) { 567 | super(source); 568 | } 569 | } 570 | } 571 | -------------------------------------------------------------------------------- /library/src/main/java/com/tr4android/recyclerviewslideitem/SwipeRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Thomas Robert Altstidl 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 | package com.tr4android.recyclerviewslideitem; 18 | 19 | public abstract class SwipeRunnable implements Runnable { 20 | int mDirection; 21 | 22 | protected SwipeRunnable(int direction) { 23 | mDirection = direction; 24 | } 25 | 26 | @Override 27 | public abstract void run(); 28 | 29 | protected int getDirection() { 30 | return mDirection; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /library/src/main/res/layout/item_swipe.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 19 | 20 | 24 | 25 | 29 | -------------------------------------------------------------------------------- /library/src/main/res/layout/item_swipe_info.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 19 | 20 | 27 | 28 | 34 | 35 | 42 | -------------------------------------------------------------------------------- /library/src/main/res/layout/item_swipe_undo.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 19 | 20 | 29 | 30 |