android - findFragmentById for a viewpager, of a tab, returns the same fragment regardless of the fragment displayed -
i have tab 2 fragments. regardless of tab selected,
getsupportfragmentmanager().findfragmentbyid(r.id.container)
returns id of fragment in second tab every time.
i experimented , added tab, repeating 1 of fragments. strange behaviour. clicking button on first fragment changes second, clicking on third changes second , clicking second changes third. swiping between tabs resets first , third fragment.
any idea how fix or work around this?
activity:
public class mainactivity extends appcompatactivity { //the {@link android.support.v4.view.pageradapter} provide fragments each of sections. private sectionspageradapter msectionspageradapter; public android.support.v4.app.fragment test; //the {@link viewpager} host section contents. private viewpager mviewpager; private activitymainbinding binding; public interface interface { public void interfacemethod(int number); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); binding = databindingutil.setcontentview(this, r.layout.activity_main); // create adapter return fragment each of 3 primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); //test button1 click behaviour binding.button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { interface interfaceuse = (interface)(getsupportfragmentmanager().findfragmentbyid(r.id.container)); interfaceuse.interfacemethod(1); } }); } protected void buttonclick(int a, int b){ int position = binding.tabs.getselectedtabposition(); } //a {@link fragmentpageradapter} returns fragment corresponding 1 of sections/tabs/pages. public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public android.support.v4.app.fragment getitem(int index) { switch (index) { case 0: return new unitfragment(); case 1: return new rmfragment(); case 2: return new unitfragment(); } return null; } @override public int getcount() { // show 2 total pages. return 3; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "unit converter"; case 1: return "one rep max"; case 2: return "dum"; } return null; } } }
unitfragment:
public class unitfragment extends fragment implements mainactivity.interface{ private textview textview1; public void interfacemethod(int position) { textview1.settext(integer.tostring(position)); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment final view rootview = inflater.inflate(r.layout.unit_fragment, container, false); //r.id textview1 = (textview) rootview.findviewbyid(r.id.textview); return rootview; } }
fix:
interface interfaceuse =(interface) getsupportfragmentmanager().findfragmentbytag("android:switcher:" + r.id.container + ":" + mviewpager.getcurrentitem());
try using code in button click,
binding.button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { interface interfaceuse = getsupportfragmentmanager().findfragmentbytag("android:switcher:" + r.id.container + ":" + mviewpager.getcurrentitem()); interfaceuse.interfacemethod(1); } });
Comments
Post a Comment