Android : Get view only on which touch was released

前端 未结 1 1858
感动是毒
感动是毒 2020-12-20 01:01

I am in a tricky situation, hope you can help me with it. I have few views (TextViews), horizontally placed one after another in a linear layout. When I press on textview1,

1条回答
  •  醉梦人生
    2020-12-20 01:48

    You need to handle all your touch events in the LinearLayout and check the location of the child views (child.getLeft() and child.getRight()).

    public boolean dispatchTouchEvent(MotionEvent event){
        int x = event.getX();
        int cc = getChildCount();
        for(int i = 0; i < cc; ++i){
            View c = getChildView();
            if(x > c.getLeft() && x < c.getRight()){
                return c.onTouchEvent(event);
            }
        }
        return false;
    }
    

    0 讨论(0)
提交回复
热议问题