Viewpager the interface is not refreshed when calling notifydatasetchanged ()
Viewpager when calling Notifydatasetchanged (), the interface does not refresh, it does affect the implementation of our function. You may choose to reset the adapter adapter for Viewpager to achieve the refresh. But this approach is problematic in most cases.
View methods
Super.notifydatasetchanged () is called by pageradapter.notifydatasetchanged ()
/** * This method should is called by the application if the data backing this adapter have changed * and Associat Ed views should update. */ Public void notifydatasetchanged () { mobservable.notifychanged (); }
Note that when the data attached to the adapter changes, the method should be called to refresh the data. The method calls a mobservable. notifychanged ();
We continue to follow this method into the Datasetobservable class and find such a piece of code:
/*** Invokes {@linkdatasetobserver#onchanged} on each observer. * Called when the contents of the data set has changed. The recipient * would obtain the new contents the next time it queries the data set. */ Public voidnotifychanged () {synchronized(mobservers) {//since onChanged () is implemented by the app, it could do anything, including//removing itself from {@link Mobservers}-and that could cause problems if//An iterator was used on the ArrayList {@link mobservers}. //to avoid such problems, just March thru the list in the reverse order. for(inti = mobservers. Size ()-1; I >= 0; i--) {mobservers.get (i). onChanged (); } } }
That's not the point. The mobservers type is an abstract class datasetobserver, there are only two non-implemented methods, who use this abstract class, which we found Viewpager figure. Entering Viewpager, we finally found the key method of controlling data change in Viewpager datasetchanged, this method is as follows:
voiddatasetchanged () {//This method only gets called if we observer is attached, so Madapter is non-null. BooleanNeedpopulate = mitems. Size () < Moffscreenpagelimit * 2 + 1 &&mitems.size ()<Madapter.getcount (); intNewcurritem =Mcuritem; BooleanIsupdating =false; for(inti = 0; I < mitems.size (); i++) { FinalItemInfo II =mitems. Get (i); Final intNewpos =madapter.getitemposition (Ii.object); if(Newpos = =pageradapter.position_unchanged) { Continue; } if(Newpos = =Pageradapter.position_none) {Mitems.remove (i); I--; if(!isupdating) {Madapter.startupdate ( This); Isupdating=true; } madapter.destroyitem ( This, Ii.position, Ii.object); Needpopulate=true; if(Mcuritem = =ii.position) {//Keep the current item in the valid rangeNewcurritem = Math. Max (0, Math.min (Mcuritem, Madapter.getcount ()-1)); Needpopulate=true; } Continue; } if(Ii.position! =Newpos) { if(Ii.position = =Mcuritem) { //Our current item changed position. Follow it.Newcurritem =Newpos; } II. Position=Newpos; Needpopulate=true; } } if(isupdating) {madapter.finishupdate ( This); } collections. Sort (Mitems, COMPARATOR); if(needpopulate) {//Reset our known page widths; populate'll recompute them. Final intChildCount =Getchildcount (); for(inti = 0; i < ChildCount; i++) { FinalView Child =Getchildat (i); FinalLayoutparams LP =(Layoutparams) child.getlayoutparams (); if(!Lp.isdecor) {LP. Widthfactor= 0. F; }} setcurrentiteminternal (Newcurritem,false,true); Requestlayout (); } }
Focus on this line of code:
Final int newpos = madapter.getitemposition (ii.object); if (Newpos = = pageradapter.position_unchanged) {continue ; }
Called when the host view was attempting to determine if a item ' s position has changed. Returns position_unchanged If the POSITION of the given item has not changed Orposition_none if the item is no longer pres ENT in the adapter.
The default implementation assumes that items would never change position and always returnsposition_unchanged.
This means that if the position of the item does not change, the position_unchanged is returned. If Position_none is returned, the item for that location does not already exist. The default implementation is to assume that the position of item will never change and return position_unchanged
Solution Solutions
So we can try to modify the adapter notation, overwriting the GetItemPosition () method, when calling Notifydatasetchanged, let the GetItemPosition method artificially return Position_none, Thus achieving the purpose of forcing Viewpager to redraw all item.
classSearchadapterextendsPageradapter {Private intMchildcount = 0; @Override Public voidnotifydatasetchanged () {Mchildcount=GetCount (); Super. notifydatasetchanged (); } @Override Public intgetitemposition (Object object) {if(Mchildcount > 0) {Mchildcount--; returnPosition_none; } return Super. GetItemPosition (object); } }I'm the dividing line of the king of the Land Tiger.
Android--processing Viewpager notifydatasetchanged No Refresh