2016-12-08 100 views
2

我正在嘗試移動和生長一個RecyclerView,以便內容佔據觸摸輸入的全屏幕基礎。我想讓RecyclerView保持左右滾動的能力。通過觸摸或手勢識別來移動RecyclerView

我無法讓GestureDetector與RecyclerView正常工作。捕捉onScrollChange不起作用,因爲它可能無法滾動。我嘗試onTouchEvent,但結果相當麻煩。有人有建議嗎?

回購:https://github.com/CubanAzcuy/Animation-Test

 mListView.setOnTouchListener(new View.OnTouchListener() { 
     Float mHistoricX = null; 
     Float mHistoricY = null; 

     Float mHistoricX2 = null; 
     Float mHistoricY2 = null; 

     int mScrollDirection = 0; 
     //1 = Left Right 
     //2 = Up Down 

     @Override 
     public boolean onTouch(View v, MotionEvent e) { 
      Log.d("TAG", "eX: " + e.getX() + " eY: " + e.getY()); 
      switch (e.getAction()) { 

        case MotionEvent.ACTION_UP: 
         Log.d("TAG", "ACTION_UP"); 

         mHistoricX = null; 
         mHistoricY = null; 
         mScrollDirection = 0; 
         break; 
        case MotionEvent.ACTION_MOVE: 
         Log.d("TAG", "ACTION_MOVE"); 
         if(mHistoricX == null || mHistoricY == null) { 
          mHistoricX = e.getX(); 
          mHistoricY = e.getY(); 
         } else { 
          if(mScrollDirection == 0) { 
           float tempX = Math.abs(mHistoricX - e.getX()); 
           float tempy = Math.abs(mHistoricY - e.getY()); 

           if(tempX >= tempy) { 
            mScrollDirection = 1; 
           } else { 
            mScrollDirection = 2; 
           } 

           mHistoricX2 = mHistoricX - e.getX(); 
           mHistoricY2 = mHistoricY - e.getY(); 

          } else { 
           mHistoricX2 = mHistoricX - e.getX(); 
           mHistoricY2 = mHistoricY - e.getY(); 
           Log.d("TAG", "X: " + mHistoricX2 + " Y: " + mHistoricY2); 

           mHistoricX = e.getX(); 
           mHistoricY = e.getY(); 
          } 

         } 
         break; 
        default: 
         break; 

       } 
      if(mScrollDirection == 2){ 
       mListView.animate().setDuration(0).xBy(-mHistoricX2).yBy(-mHistoricY2); 
       return true; 
      } 

      return false; 
     } 
    }); 

回答

2

移動下面的代碼中的一類。 {

public class OnSwipeTouchListener implements View.OnTouchListener { 

    private final GestureDetector gestureDetector; 

    public OnSwipeTouchListener (Context ctx){ 
     gestureDetector = new GestureDetector(ctx, new GestureListener()); 
    } 

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

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener { 

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

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 
     @Override 
     public boolean onSingleTapUp(MotionEvent e) { 
      onClick(); 
      return super.onSingleTapUp(e); 
     } 

     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      onDoubleClick(); 
      return super.onDoubleTap(e); 
     } 

     @Override 
     public void onLongPress(MotionEvent e) { 
      onLongClick(); 
      super.onLongPress(e); 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
        result = true; 
       } 
       else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffY > 0) { 
         onSwipeBottom(); 
        } else { 
         onSwipeTop(); 
        } 
       } 
       result = true; 

      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 

    public void onSwipeRight() { 
    } 

    public void onSwipeLeft() { 
    } 

    public void onSwipeTop() { 
    } 

    public void onSwipeBottom() { 
    } 

    public void onClick() { 

    } 

    public void onDoubleClick() { 

    } 

    public void onLongClick() { 

    } 
} 

}

使用如前所述低於這個監聽器。 將觸摸監聽器設置爲行文件的父視圖。 {

holder.relMainOfRow.setOnTouchListener(new OnSwipeTouchListener(getActivity()) { 

       @Override 
       public void onClick() { 
        super.onClick(); 

       } 

       @Override 
       public void onLongClick() { 
        super.onLongClick(); 


       } 



       public void onSwipeTop() { 

       } 

       public void onSwipeRight() { 

       } 

       public void onSwipeLeft() { 

       } 

       public void onSwipeBottom() { 

       } 
      }); 

}

+0

享受代碼....... PLZ最多投票如果妳發現有用的:) –