Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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 I moving from C to B then to E I want to identify that. Please if someone knows how to implement that can you give me an example.

Code that I have tried so far..

@Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
            switch (v.getId()) {
                case R.id.linearLayoutA:
                    Log.d(getClass().getSimpleName(), "entered A");
                    break;

                case R.id.linearLayoutB:
                    Log.d(getClass().getSimpleName(), "entered B");
                    break;

                case R.id.linearLayoutC:
                    Log.d(getClass().getSimpleName(), "entered c");
                    break;

                case R.id.linearLayoutD:
                    Log.d(getClass().getSimpleName(), "entered d");
                    break;

                case R.id.linearLayoutE:
                    Log.d(getClass().getSimpleName(), "entered e");
                    break;
            }
        } 
        return true;
    }

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...