SwipeActionView


Source link: https://github.com/Tunous/SwipeActionView

Swipe Action View

SwipeActionView is a swipe-able view, which allows users to perform actions by swiping it to the left or right side.

Table of contents

Preview

Installation

Add the JitPack repository to your root build.gradle:

allprojects {

  repositories {

maven {
 url "https://jitpack.io" 
}

  
}
 
}

And add the dependency in your module's build.gradle:

dependencies {

  compile 'com.github.Tunous:SwipeActionView:VERSION_HERE' 
}

Replace VERISION_HERE with the version you want to use. See the tags on the top of this file to see the latest available version.

Quick example

Adding SwipeActionView to your projects requires only adding it to XML and setting up SwipeGestureListener. Below example will create TextView that can be swiped both to the left or right side with two icons behind.

<me.thanel.swipeactionview.SwipeActionView
  android:id="@+id/swipe_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?colorPrimary">

<ImageView

android:layout_width="24dp"

android:layout_height="24dp"

android:src="@mipmap/ic_launcher"/>

<ImageView

android:layout_width="24dp"

android:layout_height="24dp"

android:layout_gravity="end"

android:src="@mipmap/ic_launcher"/>

  <TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="16dp"

android:text="Swipe me"/> </me.thanel.swipeactionview.SwipeActionView>
SwipeActionView swipeView = (SwipeActionView) findViewById(R.id.swipe_view);
  swipeView.setSwipeGestureListener(new SwipeGestureListener() {

  @Override
  public boolean onSwipedLeft(@NotNull SwipeActionView swipeActionView) {

showToast("Swiped left");

return true;
  
}

@Override
  public boolean onSwipedRight(@NotNull SwipeActionView swipeActionView) {

showToast("Swiped right");

return true;
  
}
 
}
);

Sample

For an example implementation of SwipeActionView see the included sample project.

You can manually compile the apk using source code or download from the releases page of this repository.

The idea

The main idea behind this library is a concept of container and background views. It allows for complete control over the look of the created views.

In the quick example section you can see that the SwipeActionView has three child views. The first two are background views, and the last one is the container. To create working version of SwipeActionView, you are only required to specify single background view and container. Second background view can be added when you want to be able to swipe in both directions.

Container

The container is a view which is drawn above other views. In the default state, this is the only visible view, and it is what gets moved when users perform swipe gestures. It can be either a simple TextView, custom view or even some sort of view group like LinearLayout. There is no limit for that, which allows you to gain complete control over the look of your views.

Background views

Background views are the mostly invisible part of SwipeActionView. They get revealed only when users start performing swipe gestures.

You can specify for which swipe direction each of them corresponds by setting their layout_gravity attribute. Default value or setting it to either left and/or start means that it will start appearing when users perform right swipe gesture. On the other hand setting it to end and/or right will result in the view to start appearing when users swipe to the left. This doesn't mean that you aren't allowed to use other layout_gravity options like center. They will still control the view as usual and will be ignored by SwipeActionView.

This behavior allows you to add single background and by specifying its layout_gravity determine whether SwipeActionView should be possible to be swiped only to the left or right side.

Note: There can be only one view for each direction. This means that if one of them is placed on the left side, the second one must be put on the right side to avoid errors.

Example

<me.thanel.swipeactionview.SwipeActionView
  android:id="@+id/swipe_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?colorPrimary">

<!-- First background view. 

  It will be located on left side of the view and will allow users to perform 

  right swipe gesture. -->
  <ImageView

android:layout_width="24dp"

android:layout_height="24dp"

android:src="@mipmap/ic_launcher"/>

<!-- Second background view. 

  This one will be located on the right side and allow users to perform 

  left swipe gesture as its layout_gravity is changed to "end". -->
  <ImageView

android:layout_width="24dp"

android:layout_height="24dp"

android:layout_gravity="end"

android:src="@mipmap/ic_launcher"/>

  <!-- Container. 

  This is what users will see when the view is not being swiped. -->
  <TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="16dp"

android:text="Swipe me"/> </me.thanel.swipeactionview.SwipeActionView>

Gesture listener

In order to be able to perform actions when users swipe the SwipeActionView you have to setup the listener with the setSwipeGestureListener(SwipeGestureListener) method. It takes SwipeGestureListener as a parameter.

SwipeGestureListener consists of two methods. One for performing an action when the view is swiped to the left and one when it is swiped to the right side.

Each of these methods returns Boolean as a result. Most of the time you'll want to return true here. Returning false is designed for advanced usage. By doing so, the view won't be automatically animated to the original position but will stay at the full translation. This allows you to manipulate the content of the visible background view. One great example of this is displaying progress wheel and manually returning the view to the original position once some long action finishes execution.

To return the view to its original position you can call the moveToOriginalPosition() method at any time.

swipeView.setSwipeGestureListener(new SwipeGestureListener() {

  @Override
  public boolean onSwipedLeft(@NotNull SwipeActionView swipeActionView) {

showToast("Swiped left");

 // Returning true automatically moves view to original position

return true;
  
}

@Override
  public boolean onSwipedRight(@NotNull SwipeActionView swipeActionView) {

showToast("Swiped right");

// Returning false requires calling moveToOriginalPosition() manually to

// reset view to original position

 // Here we are using optional parameter which will move our view to

// original position after delay in ms. In this case it's 2s.

swipeActionView.moveToOriginalPosition(2000);

 return false;
  
}
 
}
);

Disabling gestures

If you want to dynamically enable or disable gesture in a specific direction you can use the setDirectionEnabled(SwipeDirection, Boolean) method.

Note: The gesture enabling part is controlled by presence and visibility of background views. This method is only provided for convenience since it can also be changed by switching visibility of background views. It was coded like this so the specific swipe directions can be disabled by default from XML using visibility attribute on views corresponding to these directions.

swipeView.setDirectionEnabled(SwipeDirection.Left, false);

Ripple animations

SwipeActionView comes with optional support for displaying ripple animations when gestures are performed. All you have to do to enable them is to give them a color from XML or code. To do so from the code, you can use the setRippleColor(SwipeDirection, Int) method.

To disable ripple animations you can enter -1 as value for color.

swipeView.setRippleColor(SwipeDirection.Right, Color.BLUE);

Click listeners

SwipeActionView makes sure that any click listeners will work correctly. You can use setClickListener(View.OnClickListener) as usual and they should work, including views located in the container.

The only exception is that you shouldn't add click listeners for background views. This library wasn't designed to add support for this behavior. If it's possible then, that's only a positive side effect. You are better of with using libraries such as AndroidSwipeLayout instead.

Attributes

app:sav_alwaysDrawBackground="true|false"

To reduce overdraw SwipeActionView only draws parts of the main background and background views which become visible due to swipe gesture. This is not always what you want as it could break any container views with transparency. A good example where you would want to use this attribute is when you had CardView as your container.

app:sav_rippleTakesPadding="true|false"

If you want ripple effect to take padding together with container view you can set this attribute to true.

app:sav_swipeLeftRippleColor="@color"

Sets color for ripple displayed when users swipe left.

app:sav_swipeRightRippleColor="@color"

Sets color for ripple displayed when users swipe right.

Tools attributes

SwipeActionView has special attributes used only in the editor mode. They make it possible to preview ripples or contents of background views without worrying about side effects. They are entirely ignored when running on the device.

app:sav_tools_previewBackground="swipeLeft|swipeRight"

Shows background view for swipe left or right gesture.

app:sav_tools_previewRipple="swipeLeft"

Shows ripple for swipe left or right gesture.

Animations

SwipeActionView also comes with support for custom animations. There are two listeners that you can set in your code. They will be called with current swipe progress while users perform swipe gesture.

By default, there is only one animator included which scales the background views. You can use it as an example of how to implement custom animations or use it directly if it's good enough for you.

Credits

This library wouldn't be created without help and work of Brian Robles.

His KarmaMachine Reddit application was my direct inspiration when I was creating it for my application. He also created SwipeRippleDrawable and allowed me to reimplement it for purposes of this library. Huge thanks!

License

Copyright © 2016-2017 ?ukasz Rutkowski  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

Dewey is an android widget designed to hold an unlimited number of tabs, making it ideal for allowing pagination through a list.

Thin DownloadManager is an android library primary to download files and to avoid using DOWNLOAD_WITHOUT_NOTIFICATION permission when using Android provided DownloadManager in your application.

Simple image blurring for Android.

A lightweight, flexible tip dialog in Android.

This is a versatile extension for GridView.

Wasp is compact, complete, easy-in-use and all-in-one network solution. Wasp uses: Volley, Gson, OkHttp.

Wasp provides:

  • Easy implementation
  • Mocking network calls
  • Request Interceptor to add attributes (query params, headers, retry policy) to each call
  • Call based headers
  • Call based endpoint URL
  • Call based retry policy
  • Cookie management
  • Certificate management
  • Image loading in an easy way

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes