{"id":16617056,"url":"https://github.com/wdullaer/swipeactionadapter","last_synced_at":"2025-04-07T12:11:01.546Z","repository":{"id":18358932,"uuid":"21539015","full_name":"wdullaer/SwipeActionAdapter","owner":"wdullaer","description":"The Mailbox-like Swipe gesture library for Android","archived":false,"fork":false,"pushed_at":"2019-11-10T08:31:15.000Z","size":210,"stargazers_count":213,"open_issues_count":15,"forks_count":61,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-31T11:04:38.656Z","etag":null,"topics":["android","android-library","jar","listview","swipe-gestures"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wdullaer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["wdullaer"]}},"created_at":"2014-07-06T11:48:22.000Z","updated_at":"2024-03-31T14:14:59.000Z","dependencies_parsed_at":"2022-09-11T07:00:26.644Z","dependency_job_id":null,"html_url":"https://github.com/wdullaer/SwipeActionAdapter","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdullaer%2FSwipeActionAdapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdullaer%2FSwipeActionAdapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdullaer%2FSwipeActionAdapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdullaer%2FSwipeActionAdapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wdullaer","download_url":"https://codeload.github.com/wdullaer/SwipeActionAdapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648977,"owners_count":20972945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","jar","listview","swipe-gestures"],"created_at":"2024-10-12T02:15:07.050Z","updated_at":"2025-04-07T12:11:01.517Z","avatar_url":"https://github.com/wdullaer.png","language":"Java","funding_links":["https://github.com/sponsors/wdullaer"],"categories":[],"sub_categories":[],"readme":"SwipeActionAdapter - The Mailbox-like Swipe gesture library\n===========================================================\n\n![Maven Central](https://img.shields.io/maven-central/v/com.wdullaer/swipeactionadapter.svg)\n\nSwipeActionAdapter is a library that provides a simple API to create Mailbox-like action when swiping in a ListView.\nThe idea is to make it simple enough to implement while still providing sufficient options to allow you to\nintegrate it with the design of your application.\n\nSupport for Android 4.0 and up. It might work on lower API versions, but I haven't tested it and I don't intend to\nmake any effort in that direction.\n\nFeel free to fork or issue pull requests on github. Issues can be reported on the github issue tracker.\n\n![Normal Swipe Gesture](https://raw.github.com/wdullaer/SwipeActionAdapter/gh-pages/images/swipe_1.png)\n\n![Far Swipe Gesture](https://raw.github.com/wdullaer/SwipeActionAdapter/gh-pages/images/swipe_2.png)\n\nSetup\n=====\nThere are multiple options to add SwipeActionAdapter to your project:\n\n* **Grade**  \nAdd it as a dependency to your `build.gradle`\n```java\ndependencies {\n    compile 'com.wdullaer:swipeactionadapter:2.0.0'\n}\n```\n\n* **Jar File**  \n[Download the jar file](https://github.com/wdullaer/SwipeActionAdapter/releases/latest) and add it to your project\n\n* **Build from source**  \nIf you would like to get the most recent code in a jar, clone the project and run ```./gradlew jar``` from\nthe root folder. This will build a `swipeactionadapter.jar` in ```library/build/libs/```.\n\nYou may also add the library as an Android Library to your project. All the library files live in ```library```.\n\n The library also uses some Java 8 features, which Android Studio will need to transpile. This requires the following stanza in your app's `build.gradle`.\n See https://developer.android.com/studio/write/java8-support.html for more information on Java 8 support in Android.\n ```groovy\n android {\n   ...\n   // Configure only for each module that uses Java 8\n   // language features (either in its source code or\n   // through dependencies).\n   compileOptions {\n     sourceCompatibility JavaVersion.VERSION_1_8\n     targetCompatibility JavaVersion.VERSION_1_8\n   }\n }\n ```\n\nCreating your ListView with swipe actions\n-----------------------------------------\n\nIf you'd rather just start with a working example, clone the project and take a look.\n\nFor a basic implementation, you'll need to\n\n1. Wrap the Adapter of your ListView with a SwipeActionAdapter\n2. Create a background layout for each swipe direction you wish to act upon.\n3. Implement the SwipeActionAdapter\n\n### Wrap the adapter of your ListView\n\nThe majority of this libraries functionality is provided through an adapter which wraps around the content ```Adapter```\nof your ```ListView```. You will need to set the SwipeActionAdapter to your ListView and because\nwe need to set some properties of the ListView, you will also need to give a reference of the ```ListView```\nto the ```SwipeActionAdapter```.\nThis is typically done in your ```Activity```'s or ```Fragments``` onCreate/onActivityCreated callbacks.\n\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n\n    // Create an Adapter for your content\n    String[] content = new String[20];\n    for (int i=0;i\u003c20;i++) content[i] = \"Row \"+(i+1);\n    ArrayAdapter\u003cString\u003e stringAdapter = new ArrayAdapter\u003cString\u003e(\n            this,\n            R.layout.row_bg,\n            R.id.text,\n            new ArrayList\u003cString\u003e(Arrays.asList(content))\n    );\n\n    // Wrap your content in a SwipeActionAdapter\n    mAdapter = new SwipeActionAdapter(stringAdapter);\n\n    // Pass a reference of your ListView to the SwipeActionAdapter\n    mAdapter.setListView(getListView());\n\n    // Set the SwipeActionAdapter as the Adapter for your ListView\n    setListAdapter(mAdapter);\n}\n```\n\n### Create a  background layout for each swipe direction\n\nI'm just supplying an empty layout with a background for each direction.\nYou should have a layout for at least ```SwipeDirection.DIRECTION_NORMAL_LEFT``` and ```SwipeDirection.DIRECTION_NORMAL_RIGHT```.\nThe other directions are optional.\nIt is important that the background layouts scale properly vertically.\nThe ```onCreate``` callback from the previous section now becomes:\n\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n\n    // Create an Adapter for your content\n    String[] content = new String[20];\n    for (int i=0;i\u003c20;i++) content[i] = \"Row \"+(i+1);\n    ArrayAdapter\u003cString\u003e stringAdapter = new ArrayAdapter\u003cString\u003e(\n            this,\n            R.layout.row_bg,\n            R.id.text,\n            new ArrayList\u003cString\u003e(Arrays.asList(content))\n    );\n\n    // Wrap your content in a SwipeActionAdapter\n    mAdapter = new SwipeActionAdapter(stringAdapter);\n\n    // Pass a reference of your ListView to the SwipeActionAdapter\n    mAdapter.setListView(getListView());\n\n    // Set the SwipeActionAdapter as the Adapter for your ListView\n    setListAdapter(mAdapter);\n\n    // Set backgrounds for the swipe directions\n    mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)\n            .addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)\n            .addBackground(SwipeDirection.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)\n            .addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);\n}\n```\n\nLayout code\n\n```xml\n\u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:orientation=\"vertical\" android:layout_width=\"match_parent\"\n    android:layout_height=\"?android:listPreferredItemHeight\"\n    android:background=\"@android:color/holo_blue_bright\"\u003e\n\u003c/LinearLayout\u003e\n```\n\n### Implement the SwipeActionListener\n\nThe final thing to do is listen to swipe gestures. This is done by implementing the ```SwipeActionListener```.\nThis interface has three methods:\n* ```boolean hasActions(int position, SwipeDirection direction)```: return true if you want this item to be swipeable in this direction\n* ```boolean shouldDismiss(int position, SwipeDirection direction)```: return true if you want the item to be dismissed,\nreturn false if it should stay visible. This method runs on the interface thread so if you want to trigger any\nheavy actions here, put them on an ```ASyncThread```\n* ```void onSwipe(int[] position, SwipeDirection[] direction)```: triggered when all animations on the swiped items have finished.\nYou will receive an array of all swiped items, sorted in descending order with their corresponding directions.\n\nYou should pass a reference of your ```SwipeActionListener``` to the ```SwipeActionAdapter```\n\nHere's the final implementation of our example's ```onCreate``` method:\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n\n    // Create an Adapter for your content\n    String[] content = new String[20];\n    for (int i=0;i\u003c20;i++) content[i] = \"Row \"+(i+1);\n    ArrayAdapter\u003cString\u003e stringAdapter = new ArrayAdapter\u003cString\u003e(\n            this,\n            R.layout.row_bg,\n            R.id.text,\n            new ArrayList\u003cString\u003e(Arrays.asList(content))\n    );\n\n    // Wrap your content in a SwipeActionAdapter\n    mAdapter = new SwipeActionAdapter(stringAdapter);\n\n    // Pass a reference of your ListView to the SwipeActionAdapter\n    mAdapter.setListView(getListView());\n\n    // Set the SwipeActionAdapter as the Adapter for your ListView\n    setListAdapter(mAdapter);\n\n    // Set backgrounds for the swipe directions\n    mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)\n            .addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)\n            .addBackground(SwipeDirection.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)\n            .addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);\n\n    // Listen to swipes\n    mAdapter.setSwipeActionListener(new SwipeActionListener(){\n        @Override\n        public boolean hasActions(int position, SwipeDirection direction){\n            if(direction.isLeft()) return true; // Change this to false to disable left swipes\n            if(direction.isRight()) return true;\n            return false;\n        }\n\n        @Override\n        public boolean shouldDismiss(int position, SwipeDirection direction){\n            // Only dismiss an item when swiping normal left\n            return direction == SwipeDirection.DIRECTION_NORMAL_LEFT;\n        }\n\n        @Override\n        public void onSwipe(int[] positionList, SwipeDirection[] directionList){\n            for(int i=0;i\u003cpositionList.length;i++) {\n                int direction = directionList[i];\n                int position = positionList[i];\n                String dir = \"\";\n\n                switch (direction) {\n                    case SwipeDirection.DIRECTION_FAR_LEFT:\n                        dir = \"Far left\";\n                        break;\n                    case SwipeDirection.DIRECTION_NORMAL_LEFT:\n                        dir = \"Left\";\n                        break;\n                    case SwipeDirection.DIRECTION_FAR_RIGHT:\n                        dir = \"Far right\";\n                        break;\n                    case SwipeDirection.DIRECTION_NORMAL_RIGHT:\n                        AlertDialog.Builder builder = new AlertDialog.Builder(this);\n                        builder.setTitle(\"Test Dialog\").setMessage(\"You swiped right\").create().show();\n                        dir = \"Right\";\n                        break;\n                }\n                Toast.makeText(\n                        this,\n                        dir + \" swipe Action triggered on \" + mAdapter.getItem(position),\n                        Toast.LENGTH_SHORT\n                ).show();\n                mAdapter.notifyDataSetChanged();\n            }\n        }\n        \n        @Override\n        public void onSwipeStarted(ListView listView, int position, SwipeDirection direction) {\n            // User is swiping\n        }\n        \n        @Override\n        public void onSwipeEnded(ListView listView, int position, SwipeDirection direction) {\n            // User stopped swiping (lifted finger from the screen)\n        }\n    });\n}\n```\n\n\nAdditional Options\n==================\n\n### setFadeOut(boolean fadeOut)\nSetting this to true will cause the ListView item to slowly fade out as it is being swiped.\n\n### setFixedBackgrounds(boolean fixedBackgrounds)\nSetting this to true will make the backgrounds static behind the ListView item instead of sliding in from the side.\n\n### setDimBackgrounds(boolean dimBackgrounds)\nSetting this to true will make the backgrounds appear dimmed before the normal swipe threshold is reached.\n\n### setNormalSwipeFraction(float normalSwipeFraction)\nAllows you to set the fraction of the view width that must be swiped before it is counted as a normal swipe. The float must be between 0 and 1. 0 makes every swipe register, 1 effectively disables swipe.\n\n### setFarSwipeFraction(float farSwipeFraction)\nAllows you to set the fraction of the view width that must be swiped before it is counted as a far swipe. The float must be between 0 and 1. 0 makes every swipe a far swipe, 1 effectively disables a far swipe.\n\n\n### onSwipeStarted and onSwipeEnded\nYou can use these events to execute code once the user starts or stops swiping in a listItem. This can be used to fix issues \nrelating to other libraries hijacking touch events (for example; a SwipeRefreshLayout).\n```\n@Override\npublic void onSwipeStarted(ListView listView, int position, SwipeDirection direction) {\n}\n\n@Override\npublic void onSwipeEnded(ListView listView, int position, SwipeDirection direction) {\n}\n```\nLicense\n=======\n\n    Copyright (c) 2014 Wouter Dullaert\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwdullaer%2Fswipeactionadapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwdullaer%2Fswipeactionadapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwdullaer%2Fswipeactionadapter/lists"}