Android ViewPager, androidviewpager

Source: Internet
Author: User

Android ViewPager, androidviewpager

ViewPager has been used in the News client recently. Android Studio is now well integrated and automatically creates a lot of unnecessary code.

A strange thing encountered during use: the data contained in ViewPager is duplicated.

The specific figure shows:

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

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

Solved this problem according to the answer

The following describes the cause in detail:

This is the problem.

Java that will make such a mistake will certainly fail to learn how to make such a mistake ..

Let's review the static lifecycle in Java:

(1) Static local variables are defined in the function, but they do not exist when called as automatic variables, and disappear when you exit the function. Static local variables always exist, that is, their lifetime is the entire source program.
(2)Although the lifetime of a static local variable is the entire source program, its scope is still the same as that of an automatic variable. That is, the variable can only be used within the function that defines the variable.. After exiting the function, although the variable still exists, it cannot be used.
(3) allow initial values to be assigned to the static local volume of the constructor class. If the initial value is not assigned, the system automatically assigns the value 0.

The problem lies in the second article.

Because of myItem is a static variableTherefore, when the next Item is created, the Item is not destroyed or cleared but recycled by the next one. The Adapter directly displays the previous data before the current Item obtains data from the network, and the third Item is displayed after the data is retrieved from the network due to the recycling mechanism.

After removing the Item's static

Solved the problem perfectly:

 

 

The above are the problems I encountered when using ViewPager. If you have any mistakes, please correct them.

 

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.