Reputation: 3127
How to find a root view for an list item by id?
For example:
Activity layout:
<LinearLayout android:id="@+id/activity_root">
<FrameLayout android:id="@+id/activity_2">
<FrameLayout android:id="@+id/activity_3">
<ListView android:id="@android:id/list" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
List item layout:
<RelativeLayout android:id="@+id/item_root">
<FrameLayout android:id="@+id/item_2">
<FrameLayout android:id="@+id/item_3">
<Button android:id="@+id/item_button" />
</FrameLayout>
</FrameLayout>
</RelativeLayout>
Now I need to add some action on button click. All I have is button view. How do I get item_root view?
Upvotes: 1
Views: 464
Reputation: 1522
In onClick get the root view using the argument i.e view and call view.getRoot view but if its a list view than you can go for following but pass the activity i.e Activity.this to the adapter and find the root view like this:
mActivity.getWindow().getDecorView().findViewById(android.R.id.content)
Any feedback is appreciated.
Upvotes: 0
Reputation: 4569
Try this recursive function
View getParent(View view, int targetId) {
System.out.println(view.getId() +" == "+ targetId);
if (view.getId() == targetId) {
return view;
}
View parentView = (View) view.getParent();
if (parentView == null) {
return null;
}
return getParent(parentView, targetId);
}
Upvotes: 1
Reputation: 8826
you can use
view.getParent()
to find outer parent, to most outer view you need to call a few and make check control.
Upvotes: 0