Android Viewpager solutions for repetitive data problems

Source: Internet
Author: User

Recent news clients have used Viewpager,android Studio, which is now well integrated, and automatically creates a lot of code that doesn't have to be written.

In the use of the process encountered a very strange thing: Viewpager loading Data duplication

The concrete look at the picture is clear:

Can see the data loaded, 1, 2 pages are duplicated, and 3 is back to normal

After a long time, the blogger found the answer to the great God on the StackOverflow:

Because you ware using the static variable to show in fragments. When your say static their not a part of the class instance it creates only once and shared among the multiple fragment inst Ance. And you ware changing it in GetItem () method, as it is the static was reflecting in all fragment instance and repeating.

Original: Http://stackoverflow.com/questions/19042842/viewpager-showing-duplicate-data

According to this answer, this problem is solved.

Here's a detailed explanation of why:

This is where the problem lies.

Will make such a mistake Java certainly did not learn ╮ (╯_╰) ╭ like me.

Let's review the static life cycle in Java:

(1) A static local variable is defined within a function, but not as an automatic variable, when it is called and disappears when the function is exited. A static local variable always exists, that is, its lifetime is the entire source program.
(2) The lifetime of a static local variable is the entire source program, but its scope is still the same as the automatic variable, that is, the variable can only be used within the function that defines it . After exiting the function, it cannot be used even though the variable continues to exist.
(3) allows the initial value to be assigned to the static local quantity of the construction class. If the initial value is not assigned, the system automatically assigns 0 values.

The problem is the second one.

Since my item is a static variable , "referencing" the previous item,item was not destroyed or emptied at the time of the previous creation, but was recycled by the next. Before this item gets the data from the network, the previous data is displayed directly, and the third one is recycled from the network because the recycle mechanism is adapter to display the data.

When the static of the item is removed

The perfect fix for the problem:

These are the problems I encountered in the use of Viewpager, if there is anything wrong in the place also please correct me

Android Viewpager solutions for repetitive data problems

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.