Fragmentstatepageradapter.notifydatasetchanged the solution to not refresh the page

Source: Internet
Author: User

Company to do medical products, display operation is Android, so I use up and down two parts is roughly fixed, only the middle will have 6 pages of the switch, which will have two users of the switch, that is, ordinary users and administrator users, the picture can be roughly displayed

The other pages are the same, the two pages are not the same, the following is the administrator user, the thought of other pages, the middle of a viewpager, and then in order to cache multiple pages, the use of Fragmentstatepageradapter, Then through Setoffscreenpagelimit (6) to cache up to 6 pages, so that you do not have to worry about the fragment of each page of the declaration cycle of the impact of my project, this interface may not, but other interface detection, threading and UI is more complex, susceptible to fragment declaration cycles and crash.

But there is a strange request, the Administrator user's project Settings interface function is not the whole, there are two in the ordinary user over there, this design I am also very puzzled, but, still have to do ah, after, encountered the problem of the topic, Notifydatasetchanged Although there will be more and fewer pages, but the project interface is not refreshed.

Then find the degree Niang and Google, out of the answer mostly point to the same article http://www.cnblogs.com/dancefire/archive/2013/01/02/ Why-notifydatasetchanged-does-not-work.html, slightly looked at, according to the above method tried, error, still can not its essentials, and then went to see the next source, solve, share and remember this problem, lest make mistakes later

Enter notifydatasetchanged first

Find this sentence, mobservable, look at the name, the observer, should be used to monitor the change of Viewpager binding data source in real time, and then enter the Notifychanged method

Find a traversal, this traversal will go to call mobservers each element of the change, we then enter the OnChanged,

Here, find onchanged is just an abstract class in the method, since will be called, will certainly be rewritten, find a circle, in the interior of Viewpager inherit,


Hiding is still pretty deep, but this has not found we need to pay attention to the place, then continue to find, datasetchanged

void Datasetchanged () {//This method only gets called if we observer are attached, so madapter is non-null.        Final int adaptercount = Madapter.getcount ();        Mexpectedadaptercount = Adaptercount; Boolean needpopulate = Mitems.size () < Moffscreenpagelimit * 2 + 1 && mitems.size () < adapter        Count;        int newcurritem = Mcuritem;        Boolean isupdating = false;            for (int i = 0; i < mitems.size (); i++) {final ItemInfo II = Mitems.get (i);            Final int newpos = 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 range Newcurrit                    EM = Math.max (0, Math.min (Mcuritem, adapterCount-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 int childCount = Getchildcount ();   for (int i = 0; i < ChildCount; i++) {final View child = Getchildat (i);             Final Layoutparams LP = (layoutparams) child.getlayoutparams ();                if (!lp.isdecor) {lp.widthfactor = 0.F;            }} setcurrentiteminternal (Newcurritem, False, true);        Requestlayout (); }    }
Here is where we need to pay attention to, a look at so many, indeed a little headache, but, we only focus on the 13th line, there is a sentence
Final int newpos = madapter.getitemposition (Ii.object);

Here we call the GetItemPosition in our adapter, and we'll see what getitemposition will return, what it will receive, The GetItemPosition method in the replication Fragmentstatepageradapter finds only the methods in the parent class are returned

Then look at the methods in the parent class

And see what position_unchanged is for,


Mactan, this is finally understood, here has been return position_unchanged;

Return an "unchanged" flag to datasetchanged (), of course, it does not update the dead, see Datasetchanged () in the 第15-17 line


Soul Light, unexpectedly know the reason, that is good to do, the page will be refreshed directly return Position_none

@Override public    int GetItemPosition (Object object) {        if (Object.getclass (). GetName (). Equals ( ProjectFragment.class.getName ())                | | object.getclass (). GetName (). Equals (ProjectFragment2.class.getName ())) {            return position_none;        }        return Super.getitemposition (object);    }


Fragmentstatepageradapter.notifydatasetchanged the solution to not refresh the page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.