Memory optimization for Android memory optimization Viewpager

Source: Internet
Author: User

We know that in Android Viewpager is mainly used for multiple view switching, this article does not describe the basic use of Viewpager, but describes how to viewpager memory optimization. Viewpager is inherited from the viewgroup, so it will have a lot of sub-view, here is the purpose of those sub-view of the reuse, and if there is ImageView, but also the bitmap of the recycling of the introduction.

The basic principle of Viewpager

As the author's level is limited, so the basic introduction of Viewpager may not be very correct, here is the basic principle of viewpager is to facilitate the understanding of Pageradaper code.

The author thinks that Viewpager is the forgotten control of Google Dad, because all the controls involved in adapter (MVC mode), almost all of the internal implementation of Viewholder to view reuse. In a control in MVC mode, if the view is not reused, it means that each item creates a view object, and each view is ViewGroup (this is represented by ViewGroup, usually with a ListView, Viewpager), so that the view object cannot be properly recycled by the GC unless the current viewpager is recycled, or the current activity declaration cycle is over.

So in Viewpager, the reuse of view is very important.

The basic principles of some viewpager are briefly described below.

Introduction of 1.PagerAdapter

Not surprisingly, almost all controls that need to load multiple view are MVC patterns, that is, have their own adapter. In fact, this kind of control, we see a lot of, for example, Recyclerview,listview, GridView and so on.

Viewpager also belong to this category, in fact, if we want to load multiple view in Viewpager (here does not involve loading fragment), the adapter to use is Pageradapter.

Pageradapter is an abstract class, where several methods within the Pageradapter are briefly introduced.

(1). GetCount method

This method is often used to get the number of data that Viewpager needs to load, and this method is very common, with almost all of the Recyclerview and adapter in the ListView.

(2). Isviewfromobject (View view, Object object) method

This method is to determine whether the current view is related to the incoming object. What we usually write here is the return view = = object. In fact, this kind of writing is problematic. First of all, in Pageradapter each view is stored with the key value of Key-value, here is the object is the view key, the reason why we write the view = = Object Here is no error, It is because we are returning the current view in the Instantiateitem method, and the correct understanding is not returning the current view, but the current view's key. Since the current view's key and value are the same object, there is no mistake in writing this. But that's not really the way to understand it.

(3). Instantiateitem (ViewGroup container, int position) method and Destroyitem (ViewGroup container, int position, object object) Method

Here are two ways to combine the words.
First, the Instantiateitem method is used to initialize the current view, the return value is the object type, not actually the current view, but

Memory optimization for Android memory optimization Viewpager

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.