3

i use layoutparams to set the position of a few views, some are overlapped. when i touch the overlapped part, both the two views which include the overlapped part corresponded to the touch event! but i just want the above one to correspond to, can anyone can help me? thanks for answers!

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( 50, 50 );
    params.Left_AlignMargin = 100;
    params.Top_AlignMargin = 100;
    relativeLayout.addView( imgeView, params );

    params = new RelativeLayout.LayoutParams( 50, 50 );
    params.Left_AlignMargin = 50;
    params.Top_AlignMargin = 100;
    relativeLayout.addView( imageView2, params );
    //when i clicked the overlapped part, both imageview correspond to my action.how to deal with this ?
2hamed
  • 8,719
  • 13
  • 69
  • 112
ziyao_zou
  • 31
  • 3

1 Answers1

3

You can use View.OnTouchListener as the touch event on the top view. When you do this you can set the listener to return true(meaning this touch event consumes the entire touch event) and it should prevent the second(underlying) view from being affected. Example:

    topView.setOnTouchListener(new View.OnTouchListener() {


        @Override public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:


                    return true;
                case MotionEvent.ACTION_UP:{
                    //code for top view
                    return true;//this consumes the entire touch event

                }

            }
            return false;
        }
    });