在listitem上按钮的水平从右到左滑动事件onTouchlistner

时间:2013-09-29 06:22:54

标签: android swipe listitem

我想在listitem上显示用户从右向左滑动的按钮,我在适配器中写下面的代码并且它工作正常,除了它还在垂直滑动。我怎么能停止垂直滑动事件?

最终按钮btnLoyaltyName =(按钮)视图                     .findViewById(R.id.loyaltynamebutton);

        btnLoyaltyName.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int action = event.getAction();

                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    downY = event.getY();
                    return true;
                }
                case MotionEvent.ACTION_CANCEL: {
                    upX = event.getX();
                    upY = event.getY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    Log.i("afasf", "X Position==" + deltaX + "Y poastion="
                            + deltaY);

                    // if (deltaX > 0)
                    {
                        // swipe horizontal?
                        if (Math.abs(deltaX) > MIN_DISTANCE) {
                            // left or right

                            if (deltaX < 0) {
                                onSwipe(v);
                                return true;
                            }
                            if (deltaX > 0) {
                                onSwipe(v);
                                return true;
                            }
                        } else {
                            // Log.i(logTag, "Swipe was only " +
                            // Math.abs(deltaX) + " long, need at least " +
                            // MIN_DISTANCE);
                            return false; // We don't consume the event
                        }

                    }
                }
                }
                return false;

            }
        });

1 个答案:

答案 0 :(得分:0)