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 :(
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With