Android fragmentpageradapter Update Data

Source: Internet
Author: User

When using Viewpager+fragmentpageradapter, updating fragment data is not working,fragmentpageradapter Adding or reducing fragment, The previous fragment content update does not work and so on, some practice is to violently delete the fragment list, such as

list<fragment> fragments = Getsupportfragmentmanager (). getfragments (); for (int i = Fragments.size ()-1; I >= 0; i--) {Getsupportfragmentmanager (). BeginTransaction (). Remove (fragments.get (0));

There is also the practice is to rewrite the Fragmentpageradapter instantiateitem (viewgroup container, int position) method and so on, effective, although effective, but relatively coarse explosion.

Fragmentpageradapter. GetItem (int position) is not necessarily called at the time of update, so the fragment update is not valid, take a look at the code in Fragmentpageradapter's parent class Pageradapter Instantiateitem

Fragmentpageradapter in accordance with getitemid (int position) to determine whether the current position fragment exist, if present, will not be created and not updated, then let The Fragmentpageradapter update takes effect, that in Getitemid (int) returns a unique data ID based on the data, when the fragmentpageradapter is updated, the data ID is changed, Then fragment will call GetItem (int) to get the new fragment, to achieve the update effect

@Overridepublic long Getitemid (int position) {//Gets the current data of hashcode int hashcode = mdata.get (position). Hashcode (); return hashcode;}


Android fragmentpageradapter Update Data

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.