Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Swipe Items left or right

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 229
    Comment on it

    At certain application there is need to integrate swiping feature with your UI component, to make the UI more interactive. 

    Here we are taking a simple example in which one swipe the items of recycler view left or right .

    For this we used a third party library to achieve our target. So you need to compile the library in your gradle as:-

    compile "com.daimajia.swipelayout:library:1.2.0@aar"

    Step 1 (a):- create xml with swipe layout as :-

     

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <view
                android:id="@+id/rv_list"
                class="android.support.v7.widget.RecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice"
                android:divider="@color/gray"
                android:dividerHeight="0dp"
                android:visibility="visible" />
    </RelativeLayout>
    

     

    Step 1(b):- Create xml that holds items of recycler view

     

    <?xml version="1.0" encoding="utf-8"?>
    <com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/base_swiplayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--Bottom View For Right to Left-->
    
        <LinearLayout
            android:id="@+id/bottom_wrapper_delete"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/dimen_20"
            android:paddingRight="@dimen/dimen_20"
            android:background="@android:color/holo_red_dark">
            <TextView
                android:id="@+id/tvDelete"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Delete"
                android:paddingLeft="@dimen/dimen_10"
                android:paddingRight="@dimen/dimen_10"
                android:textColor="@color/white" />
        </LinearLayout>
        <!--View For Left to right-->
        <LinearLayout
            android:id="@+id/bottom_wrapper_refresh"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"/>
        </LinearLayout>
        <RelativeLayout
            android:id="@+id/notifying_row"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/dimen_20"
            android:paddingRight="@dimen/dimen_20"
            android:paddingTop="@dimen/dimen_20">
            <TextView
                android:id="@+id/tv_notifying_icon"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/dimen_10"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@drawable/fwd_ic"
                android:padding="@dimen/dimen_5"></TextView>
        </RelativeLayout>
    </com.daimajia.swipe.SwipeLayout>


    Step 2:- Get and initialize all the widgets in your main activity as:-

    swipeRow = (SwipeLayout) convertView.findViewById(R.id.base_swiplayout);
    deleteItemll=(LinearLayout) convertView.findViewById(R.id.bottom_wrapper_delete);
    refreshItemll=(LinearLayout) convertView.findViewById(R.id.bottom_wrapper_refresh);

    b:- Add following code to onBindViewHolder(CustomViewHolder holder, final int position)

    here you need to mention which layout you want to drag  left and which to right as done below

    holder.swipeRow.setShowMode(SwipeLayout.ShowMode.PullOut);
    // Drag From Left
    holder.swipeRow.addDrag(SwipeLayout.DragEdge.Right, holder.swipeRow.findViewById(R.id.bottom_wrapper_delete));
    // Drag From Right
    holder.swipeRow.addDrag(SwipeLayout.DragEdge.Left, holder.swipeRow.findViewById(R.id.bottom_wrapper_refresh));
    
    // Set setOnClickListener to both the items that swipped left and right.
    i.e deleteItemll and refreshItemll
    

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: