你的位置:首页 > 操作系统

[操作系统]Android Touch事件传递机制



 

例子一:  只有OnTouch和OnItemClick

actualListView.setOnTouchListener(new OnTouchListener() {            @Override      public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          Toast.makeText(MainActivity.this, "DOWN",               Toast.LENGTH_SHORT).show();          break;        case MotionEvent.ACTION_UP:          Toast.makeText(MainActivity.this, "UP",               Toast.LENGTH_SHORT).show();          break;        }        return false;  //返回false之后      } }); actualListView.setOnItemClickListener(new OnItemClickListener() {       @Override       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {         Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();       } });

 

先走ACTION_DOWN,ACTION_UP,最后走onItemClick。

 

 

例子二:只有OnTouch和OnItemClick

actualListView.setOnTouchListener(new OnTouchListener() {            @Override      public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          Toast.makeText(MainActivity.this, "DOWN",               Toast.LENGTH_SHORT).show();          break;        case MotionEvent.ACTION_UP:          Toast.makeText(MainActivity.this, "UP",               Toast.LENGTH_SHORT).show();          break;        }        return true;   //返回true之后 
      }
});
actualListView.setOnItemClickListener(new OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
       }
});

先走ACTION_DOWN,ACTION_UP,   onItemClick不走

 

例子三:只有OnTouch和OnItemClick和dispatchTouchEvent

@Override  public boolean dispatchTouchEvent(MotionEvent ev) {    switch (ev.getAction()) {    case MotionEvent.ACTION_DOWN:      Toast.makeText(MainActivity.this, "dispatch DOWN",           Toast.LENGTH_SHORT).show();      break;    case MotionEvent.ACTION_UP:      Toast.makeText(MainActivity.this, "dispatch UP",           Toast.LENGTH_SHORT).show();      break;    }        return super.dispatchTouchEvent(ev);  }

actualListView.setOnTouchListener(new OnTouchListener() {            @Override      public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          Toast.makeText(MainActivity.this, "DOWN",               Toast.LENGTH_SHORT).show();          break;        case MotionEvent.ACTION_UP:          Toast.makeText(MainActivity.this, "UP",               Toast.LENGTH_SHORT).show();          break;        }        return false;  //返回false之后      } });
actualListView.setOnItemClickListener(new OnItemClickListener() {       @Override       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {         Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();       } });
 

先走dispatchTouchEvent的ACTION_DOWN------------>OnTouch的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP

----------------->OnTouch的ACTION_UP----------------->onItemClick

 

 

 

 

 

例子四:只有OnTouch和OnItemClick和dispatchTouchEvent

  @Override  public boolean dispatchTouchEvent(MotionEvent ev) {    switch (ev.getAction()) {    case MotionEvent.ACTION_DOWN:      Toast.makeText(MainActivity.this, "dispatch DOWN",           Toast.LENGTH_SHORT).show();      break;    case MotionEvent.ACTION_UP:      Toast.makeText(MainActivity.this, "dispatch UP",           Toast.LENGTH_SHORT).show();      break;    }        return super.dispatchTouchEvent(ev);  }

actualListView.setOnTouchListener(new OnTouchListener() {            @Override      public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          Toast.makeText(MainActivity.this, "DOWN",               Toast.LENGTH_SHORT).show();          break;        case MotionEvent.ACTION_UP:          Toast.makeText(MainActivity.this, "UP",               Toast.LENGTH_SHORT).show();          break;        }        return true;   //返回true之后 
      }
});
actualListView.setOnItemClickListener(new OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
       }
});
 

先走dispatchTouchEvent的ACTION_DOWN------------>OnTouch的ACTION_DOWN------------->dispatchTouchEvent的ACTION_UP

----------------->OnTouch的ACTION_UP

 

 

 

 

 

 

例子五:只有OnTouch和OnItemClick和dispatchTouchEvent

  @Override  public boolean dispatchTouchEvent(MotionEvent ev) {    switch (ev.getAction()) {    case MotionEvent.ACTION_DOWN:      Toast.makeText(MainActivity.this, "dispatch DOWN",           Toast.LENGTH_SHORT).show();      return true;  //这儿无论是true还是false,都会导致OnTouch和OnItemClick不走,因为没有下面的super来分发事件    case MotionEvent.ACTION_UP:      Toast.makeText(MainActivity.this, "dispatch UP",           Toast.LENGTH_SHORT).show();      return true;  //这儿无论是true还是false,都会导致OnTouch和OnItemClick不走,因为没有下面的super来分发事件
    } 
    return super.dispatchTouchEvent(ev);
}

actualListView.setOnTouchListener(new OnTouchListener() {            @Override      public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          Toast.makeText(MainActivity.this, "DOWN",               Toast.LENGTH_SHORT).show();          break;        case MotionEvent.ACTION_UP:          Toast.makeText(MainActivity.this, "UP",               Toast.LENGTH_SHORT).show();          break;        }        return false;  //返回false之后      } });
actualListView.setOnItemClickListener(new OnItemClickListener() {       @Override       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {         Toast.makeText(MainActivity.this, "setOnClickListener", Toast.LENGTH_SHORT).show();       } });
 

先走dispatchTouchEvent的ACTION_DOWN---------------------->dispatchTouchEvent的ACTION_UP