Android Swipe Layout

Today, I came across the powerful Swipe layout Daimajia Swipe Layout.  I implemented the tables update functionality using this library only.

This is the screenshot of the activity where I used this library for showing update option.

list_tables_side

To use this library, add in dependencies:

dependencies {
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.android.support:support-v4:20.+'
    compile "com.daimajia.swipelayout:library:1.2.0@aar"
}

The layout in of table list item, using this library.

<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/swipeTables"
    swipe:rightEdgeSwipeOffset="0dp">

    <LinearLayout
        android:id="@+id/bottom_wrapper_table"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:weightSum="1">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/btnEditTable"
            app:srcCompat="@drawable/edit_category"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:clickable="true"
            android:adjustViewBounds="true"
            android:layout_gravity="center"
            android:paddingRight="20dp"
            android:paddingLeft="20dp"
            android:scaleType="fitCenter" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtTableName"
        android:maxLines="1"
        android:textColor="@color/light_black"
        android:gravity="left"
        android:textSize="16sp"
        android:text="Table Name"/>

    </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/divider_color"/>

    </LinearLayout>

</com.daimajia.swipe.SwipeLayout>

 

In the adapter, write the code:

<pre>public void onBindViewHolder(final ListTablesAdapter.SimpleViewHolder viewHolder, final int position) {
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);

 // Drag From Left
 //viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Left, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper));
 viewHolder.swipeLayout.setRightSwipeEnabled(true);
 viewHolder.swipeLayout.setLeftSwipeEnabled(false);
 // Drag From Right
 viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper_table));


 // Handling different events when swiping
 viewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
 @Override
 public void onClose(SwipeLayout layout) {
 //when the SurfaceView totally cover the BottomView.
 }

 @Override
 public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
 //you are swiping.
 }

 @Override
 public void onStartOpen(SwipeLayout layout) {

 }

 @Override
 public void onOpen(SwipeLayout layout) {
 //when the BottomView totally show.
 }

 @Override
 public void onStartClose(SwipeLayout layout) {

 }

 @Override
 public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
 //when user's hand released.
 }
 });

 // mItemManger is member in RecyclerSwipeAdapter Class
 mItemManger.bindView(viewHolder.itemView, position);
}
@Override
public int getSwipeLayoutResourceId(int position) {
    return R.id.swipeTables;
}</pre>

 

This library is great.  I am planning to use it for more purposes in the app.

Leave a comment