Android problem summary: (1) the use and cache mechanism of ViewPager in June 1.4

Source: Internet
Author: User

Android problem summary: (1) the use and cache mechanism of ViewPager in June 1.4
1. Implement lazy page loading for ViewPager;
In some cases, for example, if you use ViewPager to view multiple large charts, multiple images cannot be loaded at a time. The specific content of the page (or pre-loaded on the next page) is only loaded when you browse the page.
2. Control the number of pages cached by ViewPager.
Common situations: 1. The total number of pages is known or can be calculated. Each page occupies a small amount of resources and needs to be used frequently. This is to consider resident ViewPager instead of destruction (frequent destruction and Reconstruction also consume a large amount of resources ). 2. when a page is switched, non-adjacent pages are destroyed by default (ViewPager caches the page by default or pre-loads adjacent pages for Fast Switching). If you want to maintain the previous page status, such as the scroll position of the scroll bar, which is difficult; you can consider caching the previous pages without losing them.
Default loading and caching modes of ViewPager
ViewPager and ListView, GridView, and other data loading methods are similar. The control itself provides an Adapter interface for data loading. programmers can easily populate the data into the container by implementing a specific Adapter.
Let's look at a simple Demo.

1. ViewPager lazy loading and caching test class

Public class MainActivity extends Activity {private static final String TAG = "com. example. viewpagertest. mainActivity "; private MyViewPager viewPager; private List <View> pagers = new ArrayList <View> ();/** Number of pages cached by ViewPager; adjacent N pages on the current page will be cached */private int cachePagers = 1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getViews (); setContentView (viewPager); setListener (); setAdapter ();} private void getViews () {viewPager = new MyViewPager (this ); for (int I = 0; I <5; I ++) {TextView textView = new TextView (this); pagers. add (textView); viewPager. onDisplay (I); // Test 1} viewPager. setOffscreenPageLimit (cachePagers); // sets the cache page. Adjacent N pages on the current page are cached.} private void setAdapter () {viewPager. setAdapter (pagerAdapter);} private void setListener () {viewPager. setOnPageChangeListener (pageChangeListener);}/*** page data adapter */private PagerAdapter pagerAdapter = new PagerAdapter () {@ Override public void destroyItem (View container, int position, Object object) {Log. I (TAG, "destroyItem:" + position); (ViewGroup) container ). removeView (View) object);} @ Override public void destroyItem (ViewGroup container, int position, Object object) {Log. I (TAG, "destroyItem:" + position); container. removeView (View) object);} @ Override public Object instantiateItem (View container, int position) {Log. I (TAG, "instantiateItem:" + position); try {(ViewPager) container ). addView (pagers. get (position); // (MyViewPager) container ). onDisplay (position); // Test 2} catch (Exception e) {Log. e (TAG, e. getMessage ();} return pagers. get (position) ;}@ Override public Object instantiateItem (ViewGroup container, int position) {Log. I (TAG, "instantiateItem:" + position); try {(ViewPager) container ). addView (pagers. get (position); // (MyViewPager) container ). onDisplay (position); // Test 2} catch (Exception e) {Log. e (TAG, e. getMessage ();} return pagers. get (position) ;}@ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}@ Override public int getCount () {return pagers. size () ;}};/*** page scroll listener */private OnPageChangeListener pageChangeListener = new OnPageChangeListener () {@ Override public void onPageSelected (int arg0) {Log. I (TAG, "onPageSelected:" + arg0); // viewPager. onDisplay (arg0); // Test 3} @ Override public void onPageScrolled (int arg0, float arg1, int arg2) {}@ Override public void onPageScrollStateChanged (int arg0 ){}}; /*** @ Title setPageData * @ Description load page data * @ param position */private void setPageData (int position) {TextView textView = (TextView) pagers. get (position); textView. setText ("pager" + position); Log. I (TAG, "setPageData position:" + position);} class MyViewPager extends ViewPager implements IPagerDisplay {public MyViewPager (Context context) {super (context);} public MyViewPager (Context context Context, attributeSet attrs) {super (context, attrs) ;}@ Override public void onDisplay (int position) {setPageData (position );}}}

2. Callback interface for ViewPager Data Display

/*** @ Title IPagerDisplay. java * @ Package com. example. viewpagertest * @ Description ViewPager data display callback * @ author ze. chen * @ date 2:25:38 * @ version V1.0 */package com. example. viewpagertest;/*** @ ClassName IPagerDisplay * @ Description ViewPager lazy loading interface; this interface can be called in the instantiateItem of PagerAdapter, or * on the onPageSelected of OnPageChangeListener *, the difference between the two is that the instantiateItem method ViewPager will automatically buffer * (load pager2 data when Browsing pager1 ), * onPageSelected does not automatically buffer (pager2 data is loaded only when pager2 is browsed) * @ author ze. chen * @ date 2:25:38 **/public interface IPagerDisplay {void onDisplay (int position );}

Make ViewPager support lazy loading
In the above Code segment, the following comments are added: Test 1, Test 2, and test 3.
Test 1: Initialize all pages and data before loading ViewPager
ViewPager = new MyViewPager (this); for (int I = 0; I <5; I ++) {TextView textView = new TextView (this); pagers. add (textView); viewPager. onDisplay (I); // Test 1}

For test 2 and Test 3, only the control is added to the pagers list, and the data is not loaded immediately.
Test 2: load data during page instantiation of ViewPager. This method is also executed during pre-loading.

Public Object instantiateItem (View container, int position) {Log. I (TAG, "instantiateItem:" + position); try {(ViewPager) container ). addView (pagers. get (position); (MyViewPager) container ). onDisplay (position); // Test 2} catch (Exception e) {Log. e (TAG, e. getMessage ();} return pagers. get (position );}

Test 3: data on the page is loaded only when the page is selected. Data on the pre-loaded page is not loaded.

Private OnPageChangeListener pageChangeListener = new OnPageChangeListener () {@ Override public void onPageSelected (int arg0) {Log. I (TAG, "onPageSelected:" + arg0); viewPager. onDisplay (arg0); // Test 3 }......

Modify the number of cache pages of ViewPager

viewPager.setOffscreenPageLimit(int numbers);

The number of cached/pre-loaded pages on both sides of the current page of viewpager. When the page is switched, the numbers page on the adjacent sides of the current page will not be destroyed.


References:

Http://ranfeng0610.blog.163.com/blog/static/18570828420137206492642/

Http://zilla.blog.51cto.com/3095640/1199366

Http://blog.csdn.net/wangjinyu501/article/details/8169924

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.