View chart reuse in viewpager Adapter

Source: Internet
Author: User

Unlike the baseadapter of listview/gridview, The pageradapter of viewpager does not have an internal view reuse mechanism. That is to say, after inflate and call destroyitem, the view is discarded, if you need more views, You need to inflate them again. If all views in viewpager are basically the same, there will be a waste of memory. Here we use a very simple method to reuse the View:

List<View> mViewList = new ArrayList<View>();public Object instantiateItem(View container,  int position) {        View view = null;        if (mViewList.isEmpty) {                inflate a new View        } else {                view = mViewList.remove(0);        }        ........}public void destroyItem(View container, int position, Object object) {        View view = (View)object;        ((ViewPager) container).removeView(view);        mViewList.add(view);        ........}

In fact, it is very easy to define a list to recycle the view. When destroyitem is used, the view is added to the list. When the inflate is required, first, judge whether there are recycled views in the list. If so, reuse them. This solves the problem of constantly inflate views and then destroy.

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.