Pageradapter implementation class Encapsulation

Source: Internet
Author: User

Viewpager is a control in the android support library. It is also a control that is used in many application development scenarios. So far, for viewpager, I usually use viewpager + fragment combination, and occasionally combine viewpager and view. For the use of viewpager + fragment, the implementation of fragmentadapter can help us to develop quickly, but every time we use viewpager + view, we must inherit and implement pageradapter by ourselves, rewrite the methods we wrote once and again, so we 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, which encapsulates public operations whose content is view. * Author: msdx ([email protected]) * Time: 14-8-18 */public abstract class abstractviewpageradapter <t> extends 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, object) {return view = Object ;}@ override public object instantiateitem (viewgroup container, int position) {view = mviews. get (position); 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) {container. removeview (mviews. get (position);} public t getitem (INT position) {return mdata. get (position );}}

This class uses generics to support its extension. Two member variables are defined. One is list <t> mdata, which is used to store the data to be filled, and the other is sparsearray <View> mview, stores views in each initialized location. The key is the corresponding location.

In this abstract class, an abstract method is defined: public abstract view newview (INT position); used here to create a public abstract view if the corresponding view cannot be obtained from mview.

The implementation of the instantiateitem method is very simple, as follows:

    @Override    public Object instantiateItem(ViewGroup container, int position) {        View view = mViews.get(position);        if (view == null) {            view = newView(position);            mViews.put(position, view);        }        container.addView(view);        return view;    }

First, retrieve the corresponding view from mviews. If not, call newview (position) to create the view and add it to mviews. If so, add it to the container and return the result.

In this class, a getitem (INT position) method is added to return objects at the corresponding position.


After encapsulation, it will be very convenient to use later. inherit the class directly and implement the abstract method of newview (INT position), as follows:

    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;        }    }

Is the code much simpler?

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.