How to create an Android Tabbed Dialog containing fragments? -
can point me example or show me how create simple tabbed dialog
in android contents of each tab fragments
? examples/tutorials have found fragments , tabs, nothing specific dialogfragments
.
the documentation fragmenttabhost
shows how create tabs within normal fragments using getchildfragmentmanager()
. i'm assuming should work when fragment dialogfragment
when try get:
java.lang.illegalstateexception: fragment not have view @ android.support.v4.app.fragment$1.findviewbyid(fragment.java:1425) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:901) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1088) ...
here's code setting view (which passed alertdialog.setview()
):
private void setupview(view v) { mtabhost = (fragmenttabhost) v.findviewbyid(android.r.id.tabhost); mtabhost.setup(getactivity(), getchildfragmentmanager(), r.id.realtabcontent); mtabhost.addtab(mtabhost.newtabspec("tab1").setindicator("tab1"), mydialogfragment.class, null); }
spent lot of time working, no luck. solution found create dummy fragment tabhost , use viewpager fragments instead of tabhost fragments
voters_dialog.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" > <android.support.v4.app.fragmenttabhost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tabs" > </android.support.v4.app.fragmenttabhost> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"/> </linearlayout>
dialog class, trick use oncreateview not oncreatedialog
public class votersdialog extends dialogfragment { private fragmenttabhost mtabhost; private viewpager viewpager; private voterspageradapter adapter; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.voters_dialog, container); getdialog().settitle(getarguments().getstring("title")); mtabhost = (fragmenttabhost) view.findviewbyid(r.id.tabs); mtabhost.setup(getactivity(), getchildfragmentmanager()); mtabhost.addtab(mtabhost.newtabspec("tab1").setindicator("Плюсов"), fragment.class, null); mtabhost.addtab(mtabhost.newtabspec("tab2").setindicator("Минусов"), fragment.class, null); adapter = new voterspageradapter(getchildfragmentmanager(), getarguments()); adapter.settitles(new string[]{"Плюсов", "Минусов"}); viewpager = (viewpager)view.findviewbyid(r.id.pager); viewpager.setadapter(adapter); viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int i, float v, int i2) { } @override public void onpageselected(int i) { mtabhost.setcurrenttab(i); } @override public void onpagescrollstatechanged(int i) { } }); mtabhost.setontabchangedlistener(new tabhost.ontabchangelistener() { @override public void ontabchanged(string s) { int = mtabhost.getcurrenttab(); viewpager.setcurrentitem(i); } }); return view; } public class voterspageradapter extends fragmentpageradapter { bundle bundle; string [] titles; public voterspageradapter(fragmentmanager fm) { super(fm); } public voterspageradapter(fragmentmanager fm, bundle bundle) { super(fm); this.bundle = bundle; } @override public fragment getitem(int num) { fragment fragment = new voterslistfragment(); bundle args = new bundle(); args.putserializable("voters",bundle.getserializable( num == 0 ? "pros" : "cons")); fragment.setarguments(args); return fragment; } @override public int getcount() { return 2; } @override public charsequence getpagetitle(int position) { return titles[position]; } public void settitles(string[] titles) { this.titles = titles; } } public static class voterslistfragment extends listfragment { list<string> voters; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.list_fragment, container, false); return view; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); voters = (arraylist) getarguments().getserializable("voters"); arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, voters); setlistadapter(adapter); getlistview().setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { intent intent = new intent(getactivity(), profileactivity_.class); string login = voters.get(i); intent.putextra("login", utils.encodestring(login.substring(0, login.indexof("(")).trim())); startactivity(intent); } }); } } }
here result, can press tabs or swipe fragments
Comments
Post a Comment