i have problem in viewpager
in fragment.
when open fragment navigation view, 'listview' works in have 1 tab.
when put 2 or more tabs, wrong list view appears in second tab.
here fragment viewpager
:
public class journafragment extends fragment{ private slidingtablayout mslidingtablayout; private viewpager mviewpager; journaldate journaldate; arraylist<journaldate> montharray ; journalswipeadapter monthadapter; public journafragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_journal, null); mviewpager = (viewpager) v.findviewbyid(r.id.viewpager); journaldate = new journaldate(); montharray = journaldate.getmounthfortable(); monthadapter = new journalswipeadapter(parentactivity.getcontext(),montharray); mviewpager.setadapter(monthadapter); mslidingtablayout = (slidingtablayout) v.findviewbyid(r.id.sliding_tabs); mslidingtablayout.setviewpager(mviewpager); //system.out.println(" currentitem :" + mviewpager.getcurrentitem()); monthadapter.getlessonsforjournal(mviewpager.getcurrentitem()); monthadapter.notifydatasetchanged(); return v; } @override public void onviewcreated(view view, bundle savedinstancestate) { }
and adapter extend pageradapter:
public class journalswipeadapter extends pageradapter { private context ctx; private layoutinflater layoutinflater; arraylist<journaldate> arraylist; progressbar pg; textview title; textview nojournal; textview indexpage; listview daylistview; arraylist<journalfragmentdayitem> dayitemarraylist; journalfragmentdayitem dayitem; journalfragmentdayadapter dayadapter; public journalswipeadapter(context ctx, arraylist<journaldate> arraylist) { this.ctx = ctx; this.arraylist = arraylist; } @override public int getcount() { if (arraylist == null) { return 0; } else return arraylist.size(); } @override public boolean isviewfromobject(view view, object o) { return o == view; } public journaldate getitem(int position) { return arraylist.get(position); } @override public int getitemposition(object object) { return super.getitemposition(object); } @override public charsequence getpagetitle(int position) { switch (arraylist.get(position).calendar) { case (0): arraylist.get(position).month = "Январь"; break; case (1): arraylist.get(position).month = "Февраль"; break; case (2): arraylist.get(position).month = "Март"; break; case (3): arraylist.get(position).month = "Апрель"; break; case (4): arraylist.get(position).month = "Май"; break; case (5): arraylist.get(position).month = "Июнь"; break; case (6): arraylist.get(position).month = "Июль"; break; case (7): arraylist.get(position).month = "Август"; break; case (8): arraylist.get(position).month = "Сентябрь"; break; case (9): arraylist.get(position).month = "Октябрь"; break; case (10): arraylist.get(position).month = "Ноябрь"; break; case (11): arraylist.get(position).month = "Декабрь"; break; } return arraylist.get(position).month; } @override public object instantiateitem(viewgroup container, int position) { // inflate new layout our resources layoutinflater = (layoutinflater) ctx.getsystemservice(context.layout_inflater_service); view view = layoutinflater.inflate(r.layout.fragment_jourlan_swipe_layout, container, false); // add newly created view viewpager container.addview(view); // retrieve textview inflated view, , update it's text title = (textview) view.findviewbyid(r.id.item_title); pg = (progressbar) view.findviewbyid(r.id.progressbar); daylistview = (listview) view.findviewbyid(r.id.fragment_journal_swipe_list); nojournal = (textview)view.findviewbyid(r.id.fragment_journal_swipe_nojournal); indexpage = (textview)view.findviewbyid(r.id.item_title); view.settag("myview"+position); nojournal.setvisibility(view.gone); // return view return view; } public void getlessonsforjournal(final int position) { htmlrequest htmlrequest; requestqueue requestqueue; // system.out.println(" position: "+ position); map<string, string> params = new hashmap<>(); params.put("studentid", "5"); params.put("year", string.valueof(arraylist.get(position).year)); params.put("mounth", string.valueof(arraylist.get(position).calendar + 1)); htmlrequest = new htmlrequest(request.method.post, links.getjournallessons, params, "utf-8", new response.listener<string>() { @override public void onresponse(string response) { // system.out.println(" " +response); if (response.equals("false")) { // system.out.println("failed lessons "); } else { arraylist.get(position).responseforlessons = response; notifydatasetchanged(); dayitem = new journalfragmentdayitem(); dayitemarraylist = dayitem.getdayarray(arraylist.get(position).responseforlessons); dayadapter = new journalfragmentdayadapter(parentactivity.getcontext(), dayitemarraylist); daylistview.setadapter(dayadapter); pg.setvisibility(view.gone); daylistview.setvisibility(view.visible); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e("eror", error.getmessage() + ""); toast.maketext(parentactivity.getcontext(), "" + error.getmessage(), toast.length_long).show(); } }); htmlrequest.settag("tag"); requestqueue = volley.newrequestqueue(parentactivity.getcontext()); requestqueue.add(htmlrequest); } /** * destroy item viewpager. in our case * removing view. */ @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view) object); } }
the reason inside viewpageradapter
using same elements every page (listview
, etc...). way last instantiated page shown.
try not reusing them inside function public object instantiateitem(viewgroup container, int position)
, create them inside method.
edit
guidelines understand changes should made:
your updated class fields:
private context ctx; private layoutinflater layoutinflater; arraylist<journaldate> arraylist; arraylist<journalfragmentdayitem> dayitemarraylist; journalfragmentdayitem dayitem; journalfragmentdayadapter dayadapter;
updated method public object instantiateitem(viewgroup container, int position)
:
@override public object instantiateitem(viewgroup container, int position) { // inflate new layout our resources layoutinflater = (layoutinflater) ctx.getsystemservice(context.layout_inflater_service); view view = layoutinflater.inflate(r.layout.fragment_jourlan_swipe_layout, container, false); // add newly created view viewpager container.addview(view); // retrieve textview inflated view, , update it's text textview title = (textview) view.findviewbyid(r.id.item_title); progressbar pg = (progressbar) view.findviewbyid(r.id.progressbar); listview daylistview = (listview) view.findviewbyid(r.id.fragment_journal_swipe_list); textview nojournal = (textview) view.findviewbyid(r.id.fragment_journal_swipe_nojournal); textview indexpage = (textview) view.findviewbyid(r.id.item_title); view.settag("myview" + position); nojournal.setvisibility(view.gone); // return view return view; }
Comments
Post a Comment