How to update a view when using viewpager

Source: Internet
Author: User

Viewpager is a good thing, but often some services cannot be satisfied by Android. For example, to update a specific view of viewpager

This post is actually the summary post of stackoverflow.

Original post see http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view

A simple and quick way:

Override getItemPosition in your PagerAdapter like this:public int getItemPosition(Object object) {    return POSITION_NONE;}This way, when you call notifyDataSetChanged(), the view pager will be updated.

In fact, this method is to drop the specified position item to destroy, and then re-create a new one, which is very violent. A Daniel wrote

I think that there is no any kind of bug in the PagerAdapter, the problem is that its understanding is a little complex. Looking the solutions explained here, there is a misunderstanding and then a poor usage of instantiated views from my point of view.I have been working the last few days with PagerAdapter and ViewPager, and i found the following:The notifyDataSetChanged() method on the PagerAdapter will only notify the ViewPager that the underlying pages have changed. For example, if you have create/deleted dynamically pages (adding or removing items from your list) the ViewPager should take care about that. In this case i think that the ViewPager determines if a new view should be deleted or instantiated using the getItemPosition() and getCount() methods.I think that ViewPager after a notifyDataSetChanged() call, takes its child views, and check their position with the getItemPosition(). If for a child view this method returns POSITION_NONE, the ViewPager understand that the view has been deleted, calling the destroyItem(), and removing this view.In this way, overriding the getItemPosition() to return always POSITION_NONE is completely wrong only if you want to update the content of the pages. Because the previously created views will be destroyed, and a new ones will be created every time you call notifyDatasetChanged(). It may seem to be not so wrong just for a few TextViews, but when you have complex views, like ListViews populated with databases, this may be a real problem and a waste of resources.So there are several approaches to efficiently change the content of a view without having to remove and instantiate again the view. It the depends on the problem you want to solve. My approach is to use the setTag() method for any instantiated view in the instantiateItem() method. So when you want to change the data or invalidate the view that you need, you can call the findViewWithTag() method on the viewPager to retrieve the previously instantiated view and modify/use it as you want without having to delete create any new view each time you want to update some value.Imagine for example that you have 100 pages with 100 TextViews and you want to update only one value periodically.. with the approaches explained before it will means to remove and instantiates 100 TextView in each update. It does not make sense...

Good! It is a good idea to do something about pageradapter behavior to achieve what we want.

It is mentioned in instantiateitem (viewgroup container,
Int position), perform settag (), and then use findviewwithtag (Object Tag) to obtain the view to be operated in the location of the view to be updated. You can directly operate on The View.


However, the instantiateitem method of pageradapter writes

Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

Therefore, overwriting instantiateitem is not insurance, because the view may not be created at this time, while finishupdate () ensures that the view is available.

General Order

1. pageradapter. instantiateitem ()

2. fragmentpageradapter. getitem () -- no view is created for fragment.

3. fragmentmanager. movetostate () -- The oncreateview () of fragment is called here ()

4. pageradapter. finishupdate ()


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.