package com.example.viewpage;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MyLineaLayout extends LinearLayout implements OnGestureListener {
private GestureDetector detector;
private Context mContext;
public MyLineaLayout(Context context) {
super(context);
mContext = context;
detector = new GestureDetector(this);
}
public MyLineaLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
detector = new GestureDetector(this);
// TODO Auto-generated constructor stub
}
public void setIXClcikListener(IXClcikListener l) {
mIxClcikListener = l;
}
public boolean onDown(MotionEvent e) {
Log.e("-----", "---: on Down");
return false;
}
public void onShowPress(MotionEvent e) {
Log.e("-----", "---: on ShowPress");
}
/**
* 单击
*/
public boolean onSingleTapUp(MotionEvent e) {
Log.e("-----", "---: on SingleTapUp");
Toast.makeText(mContext, "单击", Toast.LENGTH_SHORT);
startOnClick();
return false;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.e("-----", "---: on Scroll");
return false;
}
/**
* 长按
*/
public void onLongPress(MotionEvent e) {
Log.e("-----", "---: on Long");
if (mIxClcikListener != null) {
mIxClcikListener.onLongClick();
}
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e("-----", "---: on fling");
return false;
}
private void startOnClick() {
if (mIxClcikListener != null) {
mIxClcikListener.onClick();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
// return super.onTouchEvent(event);
return true;
}
private IXClcikListener mIxClcikListener;
public interface IXClcikListener {
public void onClick();
public void onLongClick();
}
}
调用:
orderinstallA = (MyLineaLayout) view1.findViewById(R.id.ly_order_install);
orderinstallA.setIXClcikListener(new IXClcikListener() {
public void onLongClick() {
if (!mStartShake) {
mStartShake = true;
mNeedShake = true;
shakeAnimation(orderinstallA);
}
}
public void onClick() {
Toast.makeText(MainActivity.this,"单击了找优惠",Toast.LENGTH_SHORT);
startActivity(new Intent(MainActivity.this,FrmSearchCouponActivity.class));
}
});