如何结合GestureDetector检测onScroll的结束位置?

时间:2014-10-28 02:04:28

标签: android gesturedetector onscroll

我正在寻找一种从onScroll手势中检测结束位置的解决方案。 我发现just this但我不希望它在特定视图上,而不是在一个实现OnGestureListener整体的活动上。解决这个问题的最佳方法是什么?

我的代码如此票价:

public class MyActivity extends Activity implements OnGestureListener {

private GestureDetector myGesture;
private RelativeLayout rl;
private LinearLayout ll;
private LinearLayout lltest;
private HorizontalScrollView hsv;
private CardView cv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    rl = (RelativeLayout) findViewById(R.id.rl);
    ll = (LinearLayout) findViewById(R.id.ll);
    lltest = (LinearLayout) findViewById(R.id.lltest);
    cv = (CardView) findViewById(R.id.card1);
    hsv = (HorizontalScrollView) findViewById(R.id.hsv);

    myGesture =new GestureDetector(this);

    LayoutTransition transition = lltest.getLayoutTransition();
    transition.setDuration(500);
    transition.enableTransitionType(LayoutTransition.CHANGING);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return myGesture.onTouchEvent(event);
}

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

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.d("!!!!!", e1.toString());
    //Log.d("?????", e2.toString());
    if (e1.getX() > 1030) {
        Log.d("!!!!!", "Edge fling!");
        TextView text = (TextView) findViewById(R.id.text);
        text.setText("swiped!!!!!");
        HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
        hsv.setVisibility(View.VISIBLE);
    }
    return true;
}

@Override
public void onLongPress(MotionEvent e) {
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    return false;
}

public void klick(View view) {
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
        cv.getLayoutParams();
    params.height = 200;

    cv.setLayoutParams(params);
}
}

1 个答案:

答案 0 :(得分:1)

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 if(event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)
 {
   // you can get a call back here.
   return true;
 }
 return myGesture.onTouchEvent(event);
 }