How to identify hover-view when moving finger on views?

后端 未结 1 516
灰色年华
灰色年华 2021-01-16 17:29

I\'m new to android and I want to know how to identify the hover view when moving finger on different views. As a example if my activity has 5 LinearLayouts A,B,C,D,E and if

相关标签:
1条回答
  • 2021-01-16 18:10

    Implement View.OnTouchListener in your activity

    get rect area of item touch then if touch points are inside the that rectangle area then do something...

        @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                super.onWindowFocusChanged(hasFocus);
                rect1 = new Rect(linearLayout1.getLeft(), linearLayout1.getTop(),
                        linearLayout1.getRight(), linearLayout1.getBottom());
    
                rect2= new Rect(linearLayout2.getLeft(), linearLayout2.getTop(),
                        linearLayout2.getRight(), linearLayout2.getBottom());
            }
    
    /////////////////////////////
    
    @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getActionMasked() == (MotionEvent.ACTION_DOWN|MotionEvent.ACTION_MOVE)) {
                if (rect1.contains((int) event.getX(), (int) event.getY())) {
                    //do something when touch or moving on linearlayout1 area
                } else if (rect2.contains((int) event.getX(), (int) event.getY())) {
                    //do something when touch or moving on linearlayout2 area
                }
            }
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题