2015-11-27 73 views
1

任何人都可以告诉我我的代码有什么问题?我正在使用customadapter类,并在卡片堆叠中设置此适配器,当我试图刷卡时,刷卡不工作,因为DragGestureDetector未被调用。DragGestureDetector不叫

我使用这个库项目 wenchaojiang/AndroidSwipeableCardStack

这里是我的代码

private void setupAnimation(){ 
    final View cardView = viewCollection.get(viewCollection.size()-1); 


    mCardAnimator = new CardAnimator(viewCollection); 


    mCardAnimator.initLayout(); 

    final DragGestureDetector dd = new DragGestureDetector(mContext,new DragGestureDetector.DragListener(){ 

     @Override 
     public boolean onDragStart(MotionEvent e1, MotionEvent e2, 
           float distanceX, float distanceY) { 

      Log.e("CardAnimator", "Dragstsrt"); 
      mCardAnimator.drag(e1, e2, distanceX, distanceY); 
      return true; 
     } 

     @Override 
     public boolean onDragContinue(MotionEvent e1, MotionEvent e2, 
            float distanceX, float distanceY) { 
      float x1 = e1.getRawX(); 
      float y1 = e1.getRawY(); 
      float x2 = e2.getRawX(); 
      float y2 = e2.getRawY(); 

      Log.e("CardAnimator", "Dragcontjnhue"); 
      //float distance = CardUtils.distance(x1,y1,x2,y2); 
      final int direction = CardUtils.direction(x1,y1,x2,y2); 
      mCardAnimator.drag(e1,e2,distanceX,distanceY); 
      mEventListener.swipeContinue(direction, Math.abs(x2-x1),Math.abs(y2-y1)); 
      return true; 
     } 

     @Override 
     public boolean onDragEnd(MotionEvent e1, MotionEvent e2) { 
      //reverse(e1,e2); 
      float x1 = e1.getRawX(); 
      float y1 = e1.getRawY(); 
      float x2 = e2.getRawX(); 
      float y2 = e2.getRawY(); 
      float distance = CardUtils.distance(x1,y1,x2,y2); 
      final int direction = CardUtils.direction(x1,y1,x2,y2); 

      boolean discard = mEventListener.swipeEnd(direction, distance); 
      if(discard){ 

       Log.e("CardAnimator", "Dragend"); 
       mCardAnimator.discard(direction, new AnimatorListenerAdapter(){ 

        @Override 
        public void onAnimationEnd(Animator arg0) { 
         mCardAnimator.initLayout(); 
         mIndex++; 
         mEventListener.discarded(mIndex,direction); 

         //mIndex = mIndex%mAdapter.getCount(); 
         loadLast(); 

         viewCollection.get(0).setOnTouchListener(null); 
         viewCollection.get(viewCollection.size()-1) 
           .setOnTouchListener(mOnTouchListener); 
        } 

       }); 
      }else{ 
       mCardAnimator.reverse(e1,e2); 
      } 
      return true; 
     } 

     @Override 
     public boolean onTapUp() { 
      mEventListener.topCardTapped(); 
      return true; 
     } 
    } 
    ); 

    mOnTouchListener = new OnTouchListener() { 
     private static final String DEBUG_TAG = "MotionEvents"; 
     @Override 
     public boolean onTouch(View arg0, MotionEvent event) { 
      dd.onTouchEvent(event); 
      return true; 
     } 
    }; 
    cardView.setOnTouchListener(mOnTouchListener); 
} 

回答

1

@Nimit: 你应该重写onDown()并设置返回值true。你忘了方法。像这样试试,如果出现任何问题,请告诉我。

class MyGestureListener extends GestureDetector.SimpleOnGestureListener { 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
          float distanceY) { 

      Log.e(DEBUG_TAG,"Action Scroll"); 
     if(mListener == null) return true; 
     if(mStarted == false){ 
      mListener.onDragStart(e1,e2,distanceX,distanceY); 
      mStarted = true; 
     } 
     else{ 

      mListener.onDragContinue(e1,e2,distanceX,distanceY); 
     } 
     mOriginalEvent = e1; 
     return true; 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 

     return mListener.onTapUp(); 
    } 
    @Override 
    public boolean onDown(MotionEvent evt){ 
     mOriginalEvent = evt; 
     return true; 
    } 
+0

谢谢它的工作就像魅力!你救了我的一天! –