2015-11-27 112 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

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