滑动手势不工作android

时间:2015-09-23 15:51:50

标签: android

我正在开发一个ImageView,它基于所使用的手势在arraylist中渲染图像,例如左手势在阵列的位置-1上加载图像,反之亦然,在此之上是手势叠加,其用于保存和加载其他手势,如“快捷方式”到第一个图像,最后一个图像和更多,代码工作正常,直到它突然停止下面是我的代码:

SwipeListener文件

public class OnSwipeListerner implements View.OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeListerner(Context context) {
    gestureDetector = new GestureDetector(context, new GestureListener());
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_DISTANCE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float distanceX = e2.getX() - e1.getX();
        float distanceY = e2.getY() - e1.getY();
        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceX > 0)
                onSwipeRight();
            else
                onSwipeLeft();
            return true;
        }
        return false;
    }
}

我如何使用SwipeListener类:

    //swipe lock
private static final int SWIPE_LOCK = 100;
//swipe unlock
private static final int SWIPE_UNLOCK = 0;
//swipe lock key
private static final String SWIPLE_LOCK_SIG = "swipelock";
//swipe unlock key
private static final String SWIPLE_UNLOCK_SIG = "swipeunlock";
//finale sig
private int swipeSignal = 0;
.........

    imageView.setOnTouchListener(new OnSwipeListerner(this) {

        @Override
        public void onSwipeLeft() {

          if (swipeSignal == SWIPE_UNLOCK)

            {
                try {
                    ImageItem item = getData().get(imagePosition = imagePosition + 1);
                    imageView.setImageBitmap(item.getImage());
                    titleTextView.setText(item.getTitle());

                } catch (IndexOutOfBoundsException e) {
                    e.printStackTrace();
                    ImageItem item = getData().get(imagePosition = imagePosition - 1);
                    imageView.setImageBitmap(item.getImage());
                    titleTextView.setText(item.getTitle());
                }
                Log.d(DetailsActivity.this.getPackageName(), "Swipe left image View");

           }else if (swipeSignal == SWIPE_LOCK)
           {
               Toast.makeText(DetailsActivity.this, "Swipe Locked", Toast.LENGTH_SHORT).show();
           }
        }

        @Override
        public void onSwipeRight(){

            if (swipeSignal == SWIPE_UNLOCK){
            try {
                ImageItem item = getData().get(imagePosition = imagePosition - 1);
                imageView.setImageBitmap(item.getImage());
                titleTextView.setText(item.getTitle());

            }catch(IndexOutOfBoundsException e)
            {
                e.printStackTrace();
                ImageItem item = getData().get(imagePosition = imagePosition + 1);
                imageView.setImageBitmap(item.getImage());
                titleTextView.setText(item.getTitle());
            }
            Log.d(DetailsActivity.this.getPackageName(), "Swipe Right image View");


       }else if (swipeSignal == SWIPE_LOCK)
       {
           Toast.makeText(DetailsActivity.this, "Swipe Locked", Toast.LENGTH_SHORT).show();
       }
        }


    });



}

private ArrayList<ImageItem> getData() {
    final ArrayList<ImageItem> imageItems = new ArrayList<>();
    TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
    for (int i = 0; i < imgs.length(); i++) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
        imageItems.add(new ImageItem(bitmap, "Image" + i));
    }
    return imageItems;
}

@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {


ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture);

    if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
            String result = predictions.get(0).name;

            Toast.makeText(this, result, Toast.LENGTH_SHORT)
                   .show();

        if(result.equalsIgnoreCase(SWIPLE_LOCK_SIG))
        {

           // swipeSignal = SWIPE_LOCK;

        }else if(result.equalsIgnoreCase(SWIPLE_UNLOCK_SIG))
        {

          //  swipeSignal = SWIPE_UNLOCK;


        }

        }
    }

1 个答案:

答案 0 :(得分:0)

我刚刚发现问题,GestureOverlayView在XML的顶部创建了某种形式的图层,就像GestureDetector在屏幕上需要某些形式的侦听器一样,GestureOverlayView似乎阻止了GestureDetector检测到任何手势,因为它在较低的层次结构,对于GestureDetector没有收到任何手势,一切看起来都没问题,对于GestureOverlayView所有的手势都被接收,即使是它无法理解的简单滑动,因为它没有代码,它的GestureDetector有它们的代码,所以我的出路是删除GestureOverlayView