Detect which view your finger is sliding over in Android

纵饮孤独 提交于 2019-11-30 05:30:11
Garry
  • create a Layout
  • add Views to your Layout
  • set the setOnTouchListener to your Layout
  • override the onTouch method with the following:

    public boolean onTouch(View v, MotionEvent event) 
    {
       LinearLayout layout = (LinearLayout)v;
    
        for(int i =0; i< layout.getChildCount(); i++)
        {
    
            View view = layout.getChildAt(i);
            Rect outRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
            if(outRect.contains((int)event.getX(), (int)event.getY()))
            {
                                 // over a View
            }
        }
    }
    

EDIT:

I saw keyboard. I guess, it just one view and coordinates of every letter is known. So you can easily compute which letter the user slides through

AND NOW THE ANSWER:

I'm not sure, but probably this code helps your.

It's so far away, I wrote it for me. But the idea is following.

If I remember right, there is no gesturedetector for views, but you can combine touchlistener of the view with geturelistener of your activity.

Once you've touched your view, you have

private GestureDetector mGestureDetector;

// x and y coordinates within our view
private static float sideIndexX;
private static float sideIndexY;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    ...
    mGestureDetector = new GestureDetector(this, new SideIndexGestureListener());
}

class MyGestureListener extends
        GestureDetector.SimpleOnGestureListener
{
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY)
    {
        // we know already coordinates of first touch
        // we know as well a scroll distance
        sideIndexX = sideIndexX - distanceX;
        sideIndexY = sideIndexY - distanceY;

        // when the user scrolls within our side index
        // we can show for every position in it a proper
        // item in the country list
        if (sideIndexX >= 0 && sideIndexY >= 0)
        {
            doStuff();
        }

        return super.onScroll(e1, e2, distanceX, distanceY);
    }
}    

button.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // now you know coordinates of touch
        // store them
        sideIndexX = event.getX();
        sideIndexY = event.getY();

        doStuff();

        return false;
    }
});
Will Tate

You may want to try GestureDetector.

Dan

http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html it's geared to multitouch, but this is a good start toward understanding android touch/gestures, next stop, api docs/samples

The simple answer is you can't - not like the iPhone when in accessibility mode.

Until Ice Cream Sandwich that is. It now has the iPhone-like capability of being able to identify elements under your finger without having to lift it.

It's fairly straight forward to handle this manually.

Using your parent layout as the onTouchListener (in the following example, I extend a RelativeLayout), you can check for collisions between a MotionEvent and the child Views using simple co-ordinate comparison logic:

/** Returns the View colliding with the TouchEvent. */
private final View getCollisionWith(final MotionEvent pMotionEvent) {
    // Declare the LocationBuffer.
    final int[] lLocationBuffer = new int[2];
    // Iterate the children.
    for(int i = 0; i < this.getChildCount(); i++) { /** TODO: Order. */
        // Fetch the child View.
        final View lView = this.getChildAt(i);
        // Fetch the View's location.
        lView.getLocationOnScreen(lLocationBuffer);
        // Is the View colliding?
        if(pMotionEvent.getRawX() > lLocationBuffer[0] && pMotionEvent.getRawX() < lLocationBuffer[0] + lView.getWidth() && pMotionEvent.getRawY() > lLocationBuffer[1] && pMotionEvent.getRawY() < lLocationBuffer[1] + lView.getHeight()) {
           // Return the colliding View.
           return lView;
        }
    }
    // We couldn't find a colliding View.
    return null;
}

Calls to getCollisionWith will return View references that may be manipulated arbitrarily.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!