android - Fragments in Tabs of ViewPager shows wrong data -
i got following tutorial http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/ after implementing viewpager. relised though correct interfaces shown in tabs. if put toast in 3 fragments. first displays toast second , second shows toast third, , third shows no toast though has toast.
below code
public class activeseen extends appcompatactivity { // declaring view , variables toolbar toolbar; viewpager mviewpager; parseuser muser; public string objectid; public string mser; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_active_seen); toolbar = (toolbar) findviewbyid(r.id.tool_bar); setsupportactionbar(toolbar); muser = parseuser.getcurrentuser(); mser = muser.getusername(); intent = new intent(); intent geti=getintent(); objectid = geti.getstringextra("objectid"); final viewpager viewpager = (viewpager) findviewbyid(r.id.pager); setupviewpager(viewpager); tablayout tablayout = (tablayout) findviewbyid(r.id.tabanim_tabs); tablayout.setupwithviewpager(viewpager); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); switch (tab.getposition()) { case 0: // showtoast("one"); break; case 1: // showtoast("two"); break; case 2: // showtoast("three"); break; } } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); } private void setupviewpager(viewpager viewpager) { viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfrag(new criticalinformationfragment(), "critical information"); adapter.addfrag(new personalhistoryfragment(), "personal history"); adapter.addfrag(new obstetrichistoryfragment(), "obstetric history"); // adapter.addfrag(new medicalhistoryfragment(), "critical information"); // adapter.addfrag(new sexuallytransmittedinfectionfragment(), "sexually transmitted infection"); // adapter.addfrag(new familyhistoryfragment(), "family history"); viewpager.setadapter(adapter); } class viewpageradapter extends fragmentpageradapter { private final list<fragment> mfragmentlist = new arraylist<>(); private final list<string> mfragmenttitlelist = new arraylist<>(); public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } public void addfrag(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } }
and 1 of fragment classes
public class criticalinformationfragment extends fragment implements view.onclicklistener{ private static final string arg_section_number = "section_number"; textview name; textview maidenname; static textview dateofbirth; textview registrationnumber; textview bloodgrp; textview rhesusfactor; textview sicklingstatus; textview hbelectro; private floatingactionbutton fab1; activeseen mactivity; static edittext input; string objectid; parseuser muser; private progressdialog progressdialog; int color; public static criticalinformationfragment newinstance(int sectionnumber) { criticalinformationfragment fragment = new criticalinformationfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public criticalinformationfragment() { this.color = color; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_patient_info_screen_1, container, false); toast.maketext(getactivity(),"critical" , toast.length_short).show(); final floatingactionbutton fabsave = (floatingactionbutton) rootview.findviewbyid(r.id.fabsave); final floatingactionbutton fabconsult = (floatingactionbutton) rootview.findviewbyid(r.id.fabconsultation); final floatingactionbutton fabrequest = (floatingactionbutton) rootview.findviewbyid(r.id.fablabrequest); final tablerow row1 = (tablerow) rootview.findviewbyid(r.id.tablerow1); final tablerow row2 = (tablerow) rootview.findviewbyid(r.id.tablerow2); final tablerow row3 = (tablerow) rootview.findviewbyid(r.id.tablerow3); final tablerow row4 = (tablerow) rootview.findviewbyid(r.id.tablerow4); final tablerow row5 = (tablerow) rootview.findviewbyid(r.id.tablerow5); final tablerow row6 = (tablerow) rootview.findviewbyid(r.id.tablerow6); final tablerow row7 = (tablerow) rootview.findviewbyid(r.id.tablerow7); final tablerow row8 = (tablerow) rootview.findviewbyid(r.id.tablerow8); row1.setonclicklistener(this); row2.setonclicklistener(this); row3.setonclicklistener(this); row4.setonclicklistener(this); row5.setonclicklistener(this); row6.setonclicklistener(this); row7.setonclicklistener(this); row8.setonclicklistener(this); final framelayout framelayout = (framelayout) rootview.findviewbyid(r.id.frame_layout); framelayout.getbackground().setalpha(0); final floatingactionsmenu fabmenu = (floatingactionsmenu) rootview.findviewbyid(r.id.fabmenu); fabsave.setonclicklistener(this); fabconsult.setonclicklistener(this); fabrequest.setonclicklistener(this); mactivity = (activeseen) getactivity(); objectid = mactivity.objectid; name = (textview) rootview.findviewbyid(r.id.nameedit); maidenname= (textview) rootview.findviewbyid(r.id.maidenedit); dateofbirth= (textview) rootview.findviewbyid(r.id.dateofbirthedit); registrationnumber= (textview) rootview.findviewbyid(r.id.registrationedit); bloodgrp= (textview) rootview.findviewbyid(r.id.bloodgrpedit); rhesusfactor= (textview) rootview.findviewbyid(r.id.rhesusfactoredit); sicklingstatus= (textview) rootview.findviewbyid(r.id.sicklingstatusedit); hbelectro= (textview) rootview.findviewbyid(r.id.hbelectroedit); getdataparse(); fabmenu.setonfloatingactionsmenuupdatelistener(new floatingactionsmenu.onfloatingactionsmenuupdatelistener() { @override public void onmenuexpanded() { framelayout.getbackground().setalpha(300); framelayout.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { fabmenu.collapse(); return true; } }); } @override public void onmenucollapsed() { framelayout.getbackground().setalpha(0); framelayout.setontouchlistener(null); } }); return rootview; }
you calling showing toast in oncreateview. in viewpager, view created before shown. stated in answer, should override setuservisiblehint
action when fragment showing.
Comments
Post a Comment