Android Development Tips--pageradapter Implementing Class encapsulation

Source: Internet
Author: User

Viewpager is a control in the support library for Android. It is also a much more used control in many application development. So far, the use of Viewpager. I usually use a combination of Viewpager + fragment and occasionally a combination of viewpager plus view. About the use of Viewpager + fragment. There are already fragmentadapter implementations that can help us develop at a high speed, but each time we use Viewpager + view we have to inherit pageradapter and implement them. Rewrite the methods that we wrote once, and decided to encapsulate them.

The code is as follows:

/* * date:14-8-18 * project:access-control-v2 */package Cn.irains.access_control_v2.common;import Android.support.v4.view.pageradapter;import Android.util.sparsearray;import Android.view.view;import Android.view.viewgroup;import java.util.list;/** * Abstract Pageradapter Implementation class encapsulates the public operation of the content as view. * AUTHOR:MSDX ([email protected]) * time:14-8-18 pm 2:34 */public abstract class abstractviewpageradapter<t> E    Xtends pageradapter {protected list<t> mdata;    Private sparsearray<view> mviews;        Public Abstractviewpageradapter (list<t> data) {mdata = data;    Mviews = new Sparsearray<view> (Data.size ());    } @Override public int getcount () {return mdata.size ();    } @Override Public Boolean isviewfromobject (view view, Object object) {return view = = Object; } @Override Public Object instantiateitem (viewgroup container, int position) {View view = Mviews.get (positio        n); if (view = = null) {view = Newview (position);        Mviews.put (position, view);        } container.addview (view);    return view;    } public abstract View newview (int position); @Override public void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (MView    S.get (position));    } public T GetItem (int position) {return mdata.get (position); }}

In this class, generics are used to support its extended reuse.

Then there are two member variables defined, one is list<t> mdata for the data to be filled, and the other is Sparsearray<view> MView, which holds the View for each location that has been initialized, The key is its corresponding location.

In this abstract class, an abstract method is defined: Public abstract view Newview (int position), which is used when the corresponding view is not taken from Mview. Created from here.

And the implementation of the Instantiateitem method is very easy. For example, the following:

    @Override public    Object instantiateitem (viewgroup container, int position) {        View view = Mviews.get (position); C3/>if (view = = null) {            view = Newview (position);            Mviews.put (position, view);        }        Container.addview (view);        return view;    }

First remove the corresponding view from the mviews, assuming that you can call Newview (position) to create, and increase the mviews. If it is taken, it is added into the container and returned.

In this class, a new method for getitem (int position) is also added. The object to return to the appropriate location.


After encapsulation, it is very convenient to use it later, inheriting the class directly and implementing the abstract method of Newview (int position), for example the following:

    Class Openresultadapter extends Abstractviewpageradapter<openresult> {public        openresultadapter (List< Openresult> data) {            super (data);        }        @Override Public        View Newview (int position) {            View view = View.inflate (Mcontext, R.layout.view_remote_capture, NULL);            ImageView ImageView = (ImageView) View.findviewbyid (R.id.image_view);            Uiutil.setlayoutparamsheight (ImageView, R.dimen.padding_common, 4, 3);            Imageview.setadjustviewbounds (true);            Mimageloader.displayimage (Urlutil.imageurl (GetItem (position) Getimgurl ()), imageView);            return view;        }    }

Does it feel like the code is a lot simpler?

Android Development Tips--pageradapter Implementing Class encapsulation

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.