看了一下书,说一个view里,如果setOnTouchListener,那么onTouch()方法就会先于onTouchEvent()执行,
就是onTouch()会消耗了滑动事件,并返回true和不再调用onTouchEvent(),如果onTouch()返回false,表示未消耗滑动事件,然后就会调用onTouchEvent()处理。
然后我想了一想,如果onTouch()里消耗了滑动事件,但我又返回false,那么会是怎样的情况?于是我写了一下如下代码。
如下是OuterLayout extends LinearLayout,用来试验上述问题用的layout
public class OuterLayout extends LinearLayout
{
public OuterLayout(Context context)
{
this(context,null);
}
public OuterLayout(Context context,AttributeSet attrs)
{
this(context,attrs,0);
}
public OuterLayout(Context context,AttributeSet attrs,int defStyleAttr)
{
super(context,attrs,defStyleAttr);
}
public boolean onTouchEvent(MotionEvent ev)
{
switch(ev.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.e("test","now outer-layout action down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("test","now outer-layout action move");
break;
case MotionEvent.ACTION_UP:
Log.e("test","now outer-layout action up");
break;
}
return true;
}
}
在MainActivity中
LinearLayout outerLayout;
outerLayout=(LinearLayout)super.findViewById(R.id.outerlayout);
outerLayout.setOnTouchListener(this);
public boolean onTouch(View v,MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.e("test","on touch actiondown");
case MotionEvent.ACTION_MOVE:
Log.e("test","on touch action move");
case MotionEvent.ACTION_UP:
Log.e("test","on touch action up");
}
return false;
}
就是outerLayout中覆写了onTouchEvent(),并setOnTouchListener(),然后下面观察一下后台输出:
上图是布局,outerlayout是粉红色整块。