3

please see below code and say me its solution.

I have tree view same below

<RelativeLayout id="parent">

    <RelativeLayout id="container1">
       <view .../>
    </RelativeLayout>

   <RelativeLayout id="container2">
       <view .../>
    </RelativeLayout>

</RelativeLayout>

in activity:

ViewGroup parent = findViewById(R.id.parent);

parent.setOnClickListener(new ...);

I want when click on any children of parent view and children of children, parent's click event be fire.

parent view is a ViewHolder for ListView.

I test many code but not work

like, add to parent root

android:clickable="true"
android:focusable="true"

and false above for children, but :(

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ali Bagheri
  • 3,068
  • 27
  • 28
  • https://stackoverflow.com/questions/4415528/how-to-pass-a-views-onclick-event-to-its-parent-on-android – Style-7 Nov 28 '22 at 08:52

3 Answers3

2

You will need to set the click listener on each child that is added to the parent. Something like:

private void setChildListener(View parent, View.OnClickListener listener) {
    parent.setOnClickListener(listener);
    if (!(parent instanceof ViewGroup)) {
        return;
    }

    ViewGroup parentGroup = (ViewGroup) parent;
    for (int i = 0; i < parentGroup.getChildCount(); i++) {
        setChildListener(parentGroup.getChildAt(i), listener);
    }
}

This will recursively set set each view (including the initial one to the same listener.

You could also try and capture the onTouch event stealing the clicks from the subviews.

This question is a little strange to me. What behavior are you going for? When the user clicks do you want several things to happen depending on how many subviews the parent has?

J Blaz
  • 783
  • 1
  • 6
  • 26
0

Try to use OnTouchListener instead OnClickListener

ViewGroup parent = findViewById(R.id.parent);
parent.setOnTouchListener(new View.OnTouchListener {
    public boolean onTouch(View v, MotionEvent event) {
        //do smth
        return true;
    }
});
Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
0

I found problem,

I had useing android:inputType="text" for textView and after remove this, all things is ok.

when use android:inputType="text" any touch events not pass to parent, :(

Ali Bagheri
  • 3,068
  • 27
  • 28