Tutorial using PlaceHolderView
In this example we will develop views and architecture to simulate the Tinder Swipe.
Objectives of This Tutorial
- We would be creating the swipe view as used in the Tinder. Swipe right is considered accepted and swipe left is rejected.
- As we can see in the above gif that there are a lot of stuffs happening in the scene. Gestures, animations, view integrity model, dynamic data, view management and lots more. All these are implemented in the SwipePlaceHolderView class in the PlaceHolderView library.
- We will load the images from urls and set it in the view display. For this purpose we will use a library Glide.
- The profile data list will be seeded in the application and this seed json file will be stored in the assets folder.
- The seed file will be parsed into Profile object using another library gson.
- This structure will also be compatible if we are pulling url json data from a live server.
This view is a generic implementation of views in stacked form. The detail about this class can be found here
As we have been doing in my other tutorials, we will take step oriented approach to implement this view in a detailed fashion.
Let’s start the construction:
Set up the project in android studio with default activity.
In app’s build.gradle add the dependencies.
- Add an assets folder in the src/main directory and point to it in gradle assets.srcDirs
- CardView is used to display the image in the deck
Add Internet permission in the app’s AndroidManifest.xml
- Used fixed size width so get a smooth interaction behavior with the card.
- This layout will be provided as swipe state accept/reject indicator on the card.
- To avoid text wrapping issue. Make the view of same size as the card in the previous step and then provide a message text at position wherever display is required.
Place profiles.json file in the assets folder created in the above step 1.
- This strategy is very useful in bundling app with seed files. Seed files contain data build in the app package and can be used to populate database or use to display default data to the user. Placing seed files in the form of json makes is extremely easy to parse into models.
- Utils contain methods required to parse seed json file and also populate the model Profile.java
Create model Profile.java
- @SerializedName annotation belongs to gson class and used to read json file variable and bind it to the model variable.
- @Expose is used to make the variable readable to the gson
We will now create the class to bind the card view and its operations on layout.
Create TinderCard.java for the profile views.
- @layout is used to bind the layout with this class.
- @View is used to bind the views in a layout we want to refer to.
- @Resolve annotation bind a method to be executed when the view is ready to be used. Any operation we want to perform on view references should be written in a method and annotated with this.
- @SwipeOut calls the annotated method when the card has been rejected.
- @SwipeIn calls the annotated method when the card has be accepted.
- @SwipeCancelState calls the annotated method when the card is put back in the deck/canceled.
- @SwipeInState pings the annotated method till the card is moving in accepted state.
- @SwipeOutState pings the annotated method till the card is moving in rejected state.
- IMPORTANT: If we don’t plan to re add a view then the class should be annotated with @NonReusable so that the references are released and memory is optimises. For the demonstration of this tutorial we are adding a view back in the deck if rejected, so we have not used @NonReusable.
For detailed explanations view PlaceHolderView at GitHub repository
- We obtain the instance of the SwipePlaceHolderView.
- We then use the getBuilder() method to modify the default view configurations. In this example we are adding 3 cards in the display and keep adding next card when top card is removed.
- SwipeDecor class is used to adjust the visual elements of the view. Here paddingTop and relativeScale gives the perception of a card being placed in stack. The message for card stated is added through setSwipeInMsgLayoutId() and setSwipeOutMsgLayoutId().
- We load the json data and parse into Profile Object and add it to the SwipePlaceHolderView list using addView() method.
- To programmatically do swiping we call doSwipe() method with flag to indicating accepted or rejected swipe.
PlaceHolderView GitHub repository is here
Note: If you want to auto resize the cards and fix issue of card overlapping the like/dislike buttons for mobiles having bottom navigation bar. Then go through below link for the solution:
The source code for this example is here
Important developments since the 0.2.7 version:
- Dynamic view margin, Undo last swipe, Putback swiped view, Lock view
- Disable Swipe on Touch
- ItemRemovedListener added for SwipePlaceHolderView
- SwipeDirectionalView(Swipe Directions: useful for features like super like), Swipe Touch Callback, Animated Undo, Programmatically Expand/Collapse ExpandablePlaceHolderView
All the remaining release version information can be found here: Link
Learning is a journey, let’s learn together!