Android solution Viewpager load large picture memory overflow problem _android

Source: Internet
Author: User
Tags object object stub

1. We all know that for Viewpager build adapters Inherit Pageradapter, how to build it. Viewpager loads the current page and the left and right pages of the current page by default. At the beginning the current page is subscript 0, so at the beginning of the default load page No. 0 (subscript, below) and page 1th. When you slide to the right, when the current page is page 1th, Viewpager will load page 2nd, when a total of 3 pages exist (page 0,1,2). Then slide to the right, when the current page is page 2nd, the No. 0 page is removed, the 3rd page is loaded and the 3rd page is removed when you slide the current page to the left as page 1th. You should know that.

Knowing the above principle, you can let Viewpager always load only 3 pages of the picture. Loading is executing instantiateitem (...). method, remove is the execution destroyitem (...). method can be used in the instantiateitem (...). Add a picture of a page in destroyitem (...). ) to retrieve a picture of a page. Attached code:

2. code example:

Package com.example.test;
 
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.view.View;
 
Import Android.widget.ImageView;
 public class Testactivity extends activity{private Viewpager Mpager;
 Private arraylist<view> views;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Views=new arraylist<view> ();
 Views.add (This) (new ImageView);
 Views.add (This) (new ImageView);
 Views.add (This) (new ImageView);
 Views.add (This) (new ImageView);
 Views.add (This) (new ImageView);
 Views.add (This) (new ImageView);
 Views.add (This) (new ImageView);
  
 Views.add (This) (new ImageView);
 Mpager=new Viewpager (this);
 Mpager.setadapter (New Mypageradapter ());
 Setcontentview (Mpager);  Class Mypageradapter extends pageradapter{@Override public void Destroyitem (View container, int position, Object Object) {//In this recycling picture//...
 Recycle code//Removal page ((Viewpager) container). Removeview (Views.get (position)); @Override public Object Instantiateitem (View container, int position) {//pictures loaded////...
  Load picture code//Load page ((Viewpager) container). AddView (Views.get (position), 0);
 return Views.get (position);
 @Override public int GetCount () {//TODO auto-generated a stub return views.size (); @Override public boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated a stub return FA
 Lse }
  
 }
}
Related Article

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.