android - Passsing View to PageFragment -
i'm able pass string value pagefragment via bundle/instance. how can pass view ?
i tried create public linearlayout in pagefragment used pagefragment.linearlayout.addview(myview);
in fragmentpageradapter. didnt worked
public class pagefragment extends fragment { public static linearlayout linearlayout; public static pagefragment newinstance(string title) { pagefragment pagefragment = new pagefragment(); bundle bundle = new bundle(); bundle.putstring("title", title); pagefragment.setarguments(bundle); return pagefragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.page_quiz, container, false); tv = (textview) view.findviewbyid(r.id.questiontext); tv.settext(getarguments().getstring("title")); linearlayout= (linearlayout) view.findviewbyid(r.id.questionlist); return view; } }
fragmentpageradapter:
private static class myfragmentpageradapter extends fragmentpageradapter { ... @override public fragment getitem(int index) { pagefragment.linearlayout.addview(myview); //=> doesnt work string mytext = "my text"; return pagefragment.newinstance(index + mytext); }
what you've tried not correct way want. first of all, in getitem()
method of adapter fragment
's view isn't yet built linearlayout
null. if need add view fragment's view @ level should add directly in oncreateview()
callback of fragment
(passing fragment data needed populate view). otherwise create method access parent linearlayout
, add view after fragments built. keep in mind need take care of added view show again fragments may destroyed adapter.
another approach have view in fragment's layout start(visibility gone
) , toggle visibility when needed.
don't make views(like linearlayout
) static in classes may create memory leaks.
Comments
Post a Comment