码迷,mamicode.com
首页 > 移动开发 > 详细

Android Touch事件传递机制

时间:2016-04-21 11:46:37      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:


 

例子一:  只有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

Android Touch事件传递机制

标签:

原文地址:http://www.cnblogs.com/lipeineng/p/5416215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!