Is it possible to pass onTouchEvent multiple views in Android?

I read a few questions on this topic on SO, but actually did not find a reliable answer to it.

I have a frameelayout that I add up a few custom views, but the onTouch event only works with the top view. (custom views are the same view with the same onTouch event, only a few of them)

Framelayout

  • customView [2] <--- this is the last view added and the only one that receives the event
  • customView [1]
  • customView [0]

I am testing it on Android 2.2 and I wonder if there is a way so that the other looks below know where the touch happened?


EDIT (adding some code)

I am adding code that hopefully helps explain where I ran into problems. At first, I just automatically returned onTouchEventtrue. This made the last view (in my case customerView [2] ) the only one that generated the value.

However, as soon as I added a method to set the onTouchEventreturn to true or false, now the only view returning the generated value is customView [0] .

Hope this clarifies what I ask. I'm new to this, and I appreciate you taking the time to explain this (and, of course, I appreciate your patience).

In addition, I understand that my TextView does not update the value on every touchEvent, I am working on fixing this.

Activity:

public class MyActivity extend Activity {

    CustomView[] customView;
    TextView[] textView;
    int numViews 3;
    //FrameLayout and Params created

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        for(int i = 0; i < numViews; i++) {
            customView[i] = new CustomView(this, i);


            //Allows the onTouch to be handled by all Views - View[0] is the bottom view
            if(i == 0) {
                customView[i].setTouchBool(true);        //set view onTouch to return true
            } else {
                customView[i].setTouchBool(false);       //set view onTouch to return false
            }

            //Set TextView to display the number generated by the CustomView
            textView[i].setText(Double.toString(customView[i].getGeneratedNumber()));

            //Add views to main layout
            frame.addView(textView[i]);
            frame.addView(customView[i]);
        }
    }
}

My view:

public class CustomView extends View {

    boolean onTouchHandler = true;
    int xVal = 0, yVal = 0;
    int index;
    double generatedNum = 0;

    public CustomView(Context context) {
        this(context, 0);
        this.index = 0;
    }

    public CustomView(Context context, int index) {
        super(context);
        this.index = index;
    }


    @Override 
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();

        switch(action) {
            case MotionEvent.ACTION_DOWN: {
                //do logic 
            }

            case MotionEvent.ACTION_MOVE: {
                //do logic
            }

            case MotionEvent.ACTION_UP: {
                xVal = (int) ev.getX();
                yVal = (int) ev.getY();

                generateNumber(xVal, yVal, index);

                break; 
            }
        }
         return onTouchHandler;    
    }  


    private void generateNumber(int x, int y, int index) {
        if(index == 0) {
            generatedNum = (x / 2) * (y / 2) + 64;
        } else {
            generatedNum = (x / 2) * (y / 2) + (index * 128);
        }
    }

    public double getGeneratedNumber() {
        return generatedNum;
    }

    public boolean setTouchBool(boolean b) {
        this.onTouchHandler = b;
    }
}
+5
1

Android , onTouchEvent , true . , , false , .

EDIT:

Ok. , , . , , ViewGroup (View3 View2. View2 View1. View1 ParentView). , , .

, AFAIK, Android API , . , , .

, , , , , , , . , frame, onTouch.

@Override 
public boolean onTouchEvent(MotionEvent ev) {
   for(int i = 0; i < this.getChildCount(); i++){
      this.getChildAt(i).dispatchTouchEvent(ev);
   } 
   return true;
} 

, , , false, onTouch, ,

: , , , → → →

+15

All Articles