布局如下:
View 1是一个自定义的布局FlipperLayout
,View 2是一个扩展的ListView PinnedHeaderListView
,我想添加一个“pull to refresh”功能。视图1需要收听触摸事件才能向左或向右翻转。视图2也需要触摸事件。问题是,如果View 1消耗了触摸事件,则View 2无法获取它。
即使View 1消耗了该事件,我怎样才能将事件发送到View 2?
答案 0 :(得分:1)
考虑以下自定义布局:
class FlingLinearLayout extends LinearLayout {
private GestureDetector mDetector;
public FlingLinearLayout(Context context) {
super(context);
setOrientation(LinearLayout.VERTICAL);
OnGestureListener listener = new SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG, "onFling vx: " + velocityX + ", vy: " + velocityY);
if (Math.abs(velocityX) > Math.abs(velocityY)) {
String text = (velocityX > 0? "left to right" : "right to left") + " fling";
Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show();
return true;
}
return false;
}
};
mDetector = new GestureDetector(context, listener);
setBackgroundColor(0xaaff0000);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//Log.d(TAG, "onTouchEvent " + event);
// handle own events (initiated in red area)
mDetector.onTouchEvent(event);
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Log.d(TAG, "onInterceptTouchEvent " + ev);
// handle child events
// note: if you horizontally fling over button its onClick() is not performed
return mDetector.onTouchEvent(ev);
}
}
及其在您的活动中的用法:
ViewGroup ll = new FlingLinearLayout(this);
Button b;
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
Log.d(TAG, "onClick " + b.getText());
}
};
b = new Button(this);
b.setText("first");
b.setOnClickListener(l);
ll.addView(b);
b = new Button(this);
b.setText("second");
b.setOnClickListener(l);
ll.addView(b);
b = new Button(this);
b.setText("third");
b.setOnClickListener(l);
ll.addView(b);
setContentView(ll);
答案 1 :(得分:0)
touch事件返回一个布尔值,指定是否消耗了事件,只返回false,并且事件将继续隧道到子视图