在列表视图滑动时显示按钮

时间:2013-11-11 07:38:34

标签: android listview swipe

我正在尝试创建一个列表视图,如果从右侧滑动一行,则会出现一个按钮。我在Stackoverflow中浏览了类似主题的各种帖子,但无法理解这些。如果我没有掌握任何指示,那将是很好的,因为我是Android开发的新手。 在此先感谢

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试这种方式

1)创建此类

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)动画Xml文件(创建&#34;动画&#34;文件夹在&#34; res&#34;文件夹并在动画文件下创建)

<强> 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)如何使用?

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

            @Override
            public void actionSwipeDelete(int rowID) {

            }
        });