Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android Touch事件傳遞機制 一: OnTouch,OnItemClick(監聽器),dispatchTouchEvent(偽生命周期),ontouchonitemclick

Android Touch事件傳遞機制 一: OnTouch,OnItemClick(監聽器),dispatchTouchEvent(偽生命周期),ontouchonitemclick

編輯:關於android開發

Android Touch事件傳遞機制 一: OnTouch,OnItemClick(監聽器),dispatchTouchEvent(偽生命周期),ontouchonitemclick


  ViewGroup View  Activity dispatchTouchEvent 有 有 有 onInterceptTouchEvent 有 無 無 onTouchEvent 有 有 有

 

 

例子零: 只有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();
                    return false;  //這兒是true,下面是false,onItemClick會走

                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "UP", 
                            Toast.LENGTH_SHORT).show();
                    return true;  //上面隨意,只要這個地方返回true,onItemClick就不會走
                }
                return 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();
            }
            
        });

 

 

 

 

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

 

 

 

 

例子六:有OnTouch和OnItemClick    和  dispatchTouchEvent和onTouchEvent

@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);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(MainActivity.this, "EveUP", 
                Toast.LENGTH_SHORT).show();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(MainActivity.this, "Event DOWN", 
                    Toast.LENGTH_SHORT).show();
            return true;
        case MotionEvent.ACTION_UP:
            Toast.makeText(MainActivity.this, "Event UP", 
                    Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onTouchEvent(event);
    }


    Button button = (Button) findViewById(R.id.go_shopping);//這兒改為imageview結果一樣
        button.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();
                    return false;  //這兒是true,下面是false,onItemClick會走

                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "UP", 
                            Toast.LENGTH_SHORT).show();
                    return false;  //上面隨意,只要這個地方返回true,onItemClick就不會走
                }
                return false;
            }
        });
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "setOnClickListener", 
                        Toast.LENGTH_SHORT).show();                
            }
        });

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

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

activity的偽生命周期onTouchEvent沒有走,是因為button的onTouchEvent把事件消費了,

 

 

例子七:有OnTouch和OnItemClick    和  dispatchTouchEvent和onTouchEvent

 

    @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);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(MainActivity.this, "EveUP", 
                Toast.LENGTH_SHORT).show();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(MainActivity.this, "Event DOWN", 
                    Toast.LENGTH_SHORT).show();
            return true;
        case MotionEvent.ACTION_UP:
            Toast.makeText(MainActivity.this, "Event UP", 
                    Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onTouchEvent(event);
    }


        Button button = (Button) findViewById(R.id.go_shopping);  //這兒改為imageview結果一樣
        button.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();
                    return false;  //這兒是true,下面是false,onItemClick會走

                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "UP", 
                            Toast.LENGTH_SHORT).show();
                    return true;  //上面隨意,只要這個地方返回true,onItemClick就不會走
                }
                return false;
            }
        });
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "setOnClickListener", 
                        Toast.LENGTH_SHORT).show();                
            }
        });

 

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

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

button的OnClickListener沒走,是因為onTouch 在ACTION_UP中返回true,把事件給消費了,導致button的onTouchEvent沒走,連帶的button的OnClick

也沒有走。

activity的偽生命周期onTouchEvent沒有走,是因為button的onTouch把事件消費了。

 

 

 

 

例子八:有 dispatchTouchEvent和onTouchEvent

    @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);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(MainActivity.this, "EveUP", 
                Toast.LENGTH_SHORT).show();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(MainActivity.this, "Event DOWN", 
                    Toast.LENGTH_SHORT).show();
            return true;
        case MotionEvent.ACTION_UP:
            Toast.makeText(MainActivity.this, "Event UP", 
                    Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onTouchEvent(event);
    }

Button button = (Button) findViewById(R.id.go_shopping);
//不給button設置ontouch和onclick

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

activity的偽生命周期onTouchEvent沒有走,是因為button的dispatchTouchEvent------>onTouchEvent把事件消費了。即使沒有ontouch,事件也會被

button給消費掉

viewgroup(listview)和view(button)一樣,其onTouchEvent會把事件給消費了,和view(imageview)不一樣。

要想activity的偽生命周期onTouchEvent可以走的話,可以把button改為imageview,因為imageview不是“可按的CLICKABLE”,所以會imageview的onTouchEvent會return個false,不會消費掉這個事件 

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

----------------->onTouchEvent的ACTION_UP

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved