-1

I am trying to create a listview on which if a row is swiped from rightside then a button appears. I went through various post on similar topics in Stackoverflow but was unable to comprehend those. It would be great if i have few instructions to go about as I am new to Android Development. Thanks in Advance

Saraschandraa
  • 478
  • 1
  • 10
  • 28
  • http://stackoverflow.com/questions/16017988/android-list-view-right-left-swipes-like-call-logs . You can try this link.but you need to change little bit. You need to add swipe detector class to your adapter – diordna Nov 11 '13 at 07:44

2 Answers2

0

Check out the Android SwipeListView Also check Custom ListView with sliding view for each list

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Thanks GrlsHu. My requirement is not Custom Listview with sliding view and when imported Anroid SwipeListView to my IDE it shows errors. My Requirement is somewhat to this http://stackoverflow.com/questions/12713926/showing-a-delete-button-on-swipe-in-a-listview-for-android – Saraschandraa Nov 11 '13 at 07:57
0

Try this way

1) Create This class

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;



public class SmartGestureListener extends SimpleOnGestureListener implements
        OnTouchListener {

    private Context context;
    GestureDetector gDetector;
    private ListView lstCustomListView;
    private boolean isFling = false;;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private int swipeDeleteIjoomerButtonID;
    private OnSwipeDelete local;

    public SmartGestureListener() {
        super();
    }

    public SmartGestureListener(Context context, ListView lstCustomListView,
            int swipeDeleteIjoomerButtonID, OnSwipeDelete target) {
        this(context, lstCustomListView, null, swipeDeleteIjoomerButtonID, target);

    }

    public SmartGestureListener(Context context, ListView lstCustomListView,
            GestureDetector gDetector, final int swipeDeleteButttonID,
            final OnSwipeDelete target) {

        this.swipeDeleteIjoomerButtonID = swipeDeleteButttonID;
        this.local = target;

        if (gDetector == null)
            gDetector = new GestureDetector(context, this);

        this.context = context;
        this.gDetector = gDetector;
        this.lstCustomListView = lstCustomListView;

        this.lstCustomListView.setOnScrollListener(new OnScrollListener() {

            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

                if (!isFling) {

                    Log.e("tag", "onScroll");
                    int childern = SmartGestureListener.this.lstCustomListView
                            .getChildCount();
                    Log.e("tag", "count : " + childern);
                    for (int i = 0; i < childern; i++) {
                        View v = SmartGestureListener.this.lstCustomListView
                                .getChildAt(i);
                        Log.e("tag", "inside for loop");
                        if (v != null) {
                            Button btnDelete = (Button) v
                                    .findViewById(swipeDeleteButttonID);
                            if (btnDelete != null) {
                                Log.e("tag", "IjoomerButton found");
                                if (btnDelete.getVisibility() == View.VISIBLE) {
                                    btnDelete.setVisibility(View.INVISIBLE);
                                } else {
                                    btnDelete.setVisibility(View.INVISIBLE);
                                }

                            } else {
                                Log.e("tag", "IjoomerButton not found");
                            }
                        }
                        {
                            Log.e("tag", "view not found");
                        }
                    }
                } else {
                    isFling = false;
                }
            }
        });
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        super.onFling(e1, e2, velocityX, velocityY);

        try {
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                isFling = true;
                if (lstCustomListView != null) {
                    final int adapterIndex = lstCustomListView.pointToPosition(
                            (int) e1.getX(), (int) e1.getY());
                    int firstViewItemIndex = lstCustomListView
                            .getFirstVisiblePosition();
                    int viewIndex = adapterIndex - firstViewItemIndex;
                    Log.e("indexes", adapterIndex + " : " + firstViewItemIndex
                            + " : " + viewIndex);

                    View v = lstCustomListView.getChildAt(viewIndex);
                    Button btnDelete = (Button) v
                            .findViewById(this.swipeDeleteIjoomerButtonID);

                    // flip in animation applied to the IjoomerButton

                    if (btnDelete.getVisibility() == View.INVISIBLE) {

                        btnDelete.setVisibility(View.VISIBLE);
                        Animation a = AnimationUtils.loadAnimation(context,
                                R.anim.slide_left_in);
                        a.reset();
                        btnDelete.clearAnimation();
                        btnDelete.startAnimation(a);

                        btnDelete.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {


                                local.actionSwipeDelete(adapterIndex);
                            }
                        });
                    } else {
                        btnDelete.setVisibility(View.INVISIBLE);
                    }

                }
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                isFling = true;

                if (lstCustomListView != null) {
                    final int adapterIndex = lstCustomListView.pointToPosition(
                            (int) e1.getX(), (int) e1.getY());
                    int firstViewItemIndex = lstCustomListView
                            .getFirstVisiblePosition();
                    final int viewIndex = adapterIndex - firstViewItemIndex;
                    Log.e("indexes", adapterIndex + " : " + firstViewItemIndex
                            + " : " + viewIndex);

                    View v = lstCustomListView.getChildAt(viewIndex);
                    Button btnDelete = (Button) v
                            .findViewById(swipeDeleteIjoomerButtonID);

                    if (btnDelete.getVisibility() == View.INVISIBLE) {

                        btnDelete.setVisibility(View.VISIBLE);

                        Animation a = AnimationUtils.loadAnimation(context,
                                R.anim.slide_left_in);
                        a.reset();
                        btnDelete.clearAnimation();
                        btnDelete.startAnimation(a);

                        btnDelete.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {

                                    local.actionSwipeDelete(adapterIndex);
                            }
                        });
                    } else {
                        btnDelete.setVisibility(View.INVISIBLE);
                    }

                }
                return true;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return super.onSingleTapConfirmed(e);
    }

    public boolean onTouch(View v, MotionEvent event) {
        Log.e("gesture", "onTouch");
        return gDetector.onTouchEvent(event);
    }

    public GestureDetector getDetector() {
        return gDetector;
    }

}

2) Animation Xml file (Create "anim" folder in "res" folder and create below animation file)

slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>
</set>

3) How to use?

SmartGestureListener swipeGestureListener = new  SmartGestureListener(this, myListview, R.id.btnDelete, new OnSwipeDelete() {

            @Override
            public void actionSwipeDelete(int rowID) {

            }
        });
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77