android - Refreshing fragments in ViewPager -
i have fragment activity viewpager in it, , fragment activity should send data fragments inside of pager(so fragments can display search results). so issue when swipe fragment doesnt refresh - need swipe few more times between fragments show new data. can tell me propper approach this?
here code:
//first init widgets private void initwidgets() { bundle = new bundle(); progressbar = (progressbar) findviewbyid(r.id.search_progressbar); viewpager = (viewpager) findviewbyid(r.id.search_viewpager); titlestrip = (pagertabstrip) findviewbyid(r.id.search_titlestrip); myfragmentpageradapter = new screenslidepageradapter(getsupportfragmentmanager()); viewpager.setadapter(myfragmentpageradapter); viewpager.setcurrentitem(1001); titlestrip.setdrawfullunderline(true); } //then after writing text in search call function// private void search(string keyword) { try { bundle.putstring(intentconstants.keyword, searchet.gettext().tostring()); myfragmentpageradapter.notifydatasetchanged(); } catch (exception e) { e.printstacktrace(); } } /*and bundle passed fragments - in subclass of fragmentactivity*/ private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch (position % page_titles.length) { // community case 0: communitysearchfragment f = new communitysearchfragment(); f.setarguments(bundle); return f; //my clipbords clipboardadater fragmentsearchmyclipboards case 1: fragmentsearchmyclipboards fragment = new fragmentsearchmyclipboards(); fragment.setarguments(bundle); return fragment; // clips case 2: myclipsfragment f2 = new myclipsfragment(); f2.setarguments(bundle); return f2; // members case 3: membersfragment f3 = new membersfragment(); f3.setarguments(bundle); return f3; } return null; } @override public charsequence getpagetitle(int position) { return page_titles[position % page_titles.length]; } @override public int getcount() { return 5000; } }
and here 1 of fragments reference do:
public class myclipsfragment extends fragment { arraylist<clipsvo> clipsarray; private view mview; private listview list; private myclipboardsearchadapter adapter; private arraylist<clipsvo> rclipsarray; private progressbar progress; @override public void onapiresponse(resultvo result) { super.onapiresponse(result); if (result.getapimethod().equals(clipixserver.apimethod_searchclip)) { clipsarray = (arraylist<clipsvo>) result.getdata(); int userid = integer.parseint(userprofile.getinstance().getuserdata().getuserid()); if (clipsarray.size() > 0) { arraylist<clipsvo> rclipsarray = new arraylist<clipsvo>(); (int = 0; < clipsarray.size(); i++) { clipsvo clip = clipsarray.get(i); if (clip.getowneruserid().equals(userid + "")) { if (rclipsarray.size() == 0) { //add header current user's clips in result clipsvo headerclip = new clipsvo(); headerclip.setheader(true); headerclip.setdescription(getlocalizationstring("lblcommunitymyclips")); rclipsarray.add(headerclip); } rclipsarray.add(clipsarray.remove(i)); i--; } } if (clipsarray.size() > 0) { //add header other clipboards in result clipsvo headerclip = new clipsvo(); headerclip.setheader(true); headerclip.setdescription(getlocalizationstring("lblclips")); rclipsarray.add(headerclip); rclipsarray.addall(clipsarray); } } this.rclipsarray = clipsarray; if (getactivity() != null) setupdata(); } } private void setupdata() { adapter = new myclipboardsearchadapter(getactivity(), rclipsarray); list.setadapter(adapter); textview emptytxt = (textview) mview.findviewbyid(r.id.empty); emptytxt.settext(getlocalizationstring("txtpeoplesearchnoresult")); progress.setvisibility(view.gone); emptytxt.settextsize(14); list.setemptyview(emptytxt); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { object itemobject = rclipsarray.get(position); arraylist<clipsvo> clipslist = new arraylist<clipsvo>(); clipsvo clipvo = (clipsvo) itemobject; clipslist.add(clipvo); intent intent = new intent(getactivity(), clipslistdetailactivity.class); intent.putextra(intentconstants.clip_data, clipvo); intent.putextra(intentconstants.clip_id, clipvo.getclipid()); intent.putextra(intentconstants.is_userclips, false); userprofile.getinstance().setclipslist(clipslist); intent.putextra(intentconstants.clip_index_position, 0); intent.putextra(intentconstants.clipboard_index_position, 0); startactivity(intent); } }); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mview = inflater.inflate(r.layout.fragment_list_layout, container, false); list = (listview) mview.findviewbyid(r.id.fragment_list_layout_list); progress = (progressbar) mview.findviewbyid(r.id.frag_progress); progress.setvisibility(view.visible); rclipsarray = new arraylist<clipsvo>(); if (getarguments().getstring(intentconstants.keyword) != null) { //my clips hashmap<string, string> params = new hashmap<string, string>(); setuserdata(params); params.put(clipixserver.feedparams_keyword, getarguments().getstring(intentconstants.keyword)); params.put(clipixserver.feedparams_resultcount, "100"); placerequest(clipixserver.apimethod_searchclip, json.methodname_parseclips, params); } else { progress.setvisibility(view.gone); } return mview; } }
method 1:
viewpager loads fragment default 1 in both sides. so, can have 3 fragments loaded in adapter.
try using method below
public void setoffscreenpagelimit (int limit)
as
viewpager.setoffscreenpagelimit(0);
this allow load fragments dynamically.
method 2:
on load of fragment [onattachactivity] can refresh view [set view contents]
Comments
Post a Comment