Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在滑动Listview时显示按钮_Android_Listview_Swipe - Fatal编程技术网

Android 在滑动Listview时显示按钮

Android 在滑动Listview时显示按钮,android,listview,swipe,Android,Listview,Swipe,我试图创建一个listview,如果从右侧滑动一行,则会出现一个按钮。我在Stackoverflow上浏览了许多关于类似主题的帖子,但无法理解。这将是伟大的,如果我有一些指示去做,因为我是新的Android开发。 提前感谢请查看并检查试试这种方法 1) 创建这个类 import android.content.Context; import android.util.Log; import android.view.GestureDetector; import android.view.Ges

我试图创建一个listview,如果从右侧滑动一行,则会出现一个按钮。我在Stackoverflow上浏览了许多关于类似主题的帖子,但无法理解。这将是伟大的,如果我有一些指示去做,因为我是新的Android开发。 提前感谢

请查看并检查

试试这种方法

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;
    }

}

. 你可以试试这个链接,但是你需要稍微改变一下。您需要将滑动检测器类添加到适配器Hanks GrlsHu。我的要求不是带有滑动视图的自定义Listview,当将一个Roid SwipeListView导入我的IDE时,它会显示错误。我的要求与此有些出入
<?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>
SmartGestureListener swipeGestureListener = new  SmartGestureListener(this, myListview, R.id.btnDelete, new OnSwipeDelete() {

            @Override
            public void actionSwipeDelete(int rowID) {

            }
        });