android - get the fragment that includes a particular view -
how find fragment includes particular view?
it easy find view knowing fragment , view id:
fragment.getview().findviewbyid(r.id.foo)
but want reversal: knowing view, find fragment.
so far best thought is:
i. set root view's tag fragment inflated it:
// in class extends fragment: @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_id, container, false); rootview.settag(this); // <======= !!! homecoming rootview; }
ii. then, it's relatively easy find root view among view's parents:
// in utility class: public static fragment gettagfragment(view view) { (view v = view; v != null; v = (v.getparent() instanceof view) ? (view)v.getparent() : null) { object tag = v.gettag(); if (tag != null && tag instanceof fragment) { homecoming (fragment)tag; } } homecoming null; }
iii. usage:
fragment f = gettagfragment(view);
ps can place onclick handler fragment, , works!
android android-fragments
No comments:
Post a Comment