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.