有人可以告诉我我的代码有什么问题吗?我正在使用customadapter类,当我尝试刷卡时,我将此适配器设置在cardstack中,滑动不起作用,因为没有调用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);
}
答案 0 :(得分:0)
@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;
}