Android development skills -- simple encapsulation of PagerAdapter, pageradapter

Source: Internet
Author: User

Android development skills -- simple encapsulation of PagerAdapter, pageradapter

This time, we will encapsulate the adapter PagerAdapter of ViewPager whose content is View. It supports List data and SparseArray data and provides the View update function.

First, paste the top abstract class code:

/** 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 (645079761@qq.com) * Time: 14-8-18 */public abstract class extends actviewpageradapter extends PagerAdapter {protected SparseArray <View> mViews; public extends actviewpageradapter () {mViews = new SparseArray <View> () ;}@ Override public boolean isViewFromObject (View view, Object 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); public void policyupdateview (int position) {View view = updateView (mViews. get (position), position); mViews. put (position, view); notifyDataSetChanged ();} public View updateView (View view, int position) {return view ;}@ Override public void destroyItem (ViewGroup container, int position, object object) {container. removeView (mViews. get (position ));}}

Here, only View-related operations are encapsulated, no data is involved, and two methods are added. One is yyupdateview (int position ), after calling it, the view at the specified position will be updated. The other method is public View updateView (View view, int position), where you can update the View.


Next we will look at the encapsulation of data as List. The Code is as follows:

/** Date: 14-8-18 * Project: Access-Control-V2 */package cn. irains. access_control_v2.common; import android. view. view; import java. util. list;/*** abstract PagerAdapter implementation class, which encapsulates the implementation of an adapter whose content is View and whose data is List. * Author: msdx (645079761@qq.com) * Time: 14-8-18 */public abstract class AbstractPagerListAdapter <T> extends AbstractViewPagerAdapter {protected List <T> mData; public AbstractPagerListAdapter (List <T> data) {mData = data ;}@ Override public int getCount () {return mData. size ();} public abstract View newView (int position); public T getItem (int position) {return mData. get (position );}}

Methods are basically the same as those in the previous article, but some methods are extracted to the parent class and then inherited from the class.

Similarly, the adapter encapsulation class code with data as SparseArray is similar to the following:

/** Date: 14-8-18 * Project: Access-Control-V2 */package cn. irains. access_control_v2.common; import android. util. sparseArray; import android. view. view;/*** abstract PagerAdapter implementation class. It encapsulates the implementation of an adapter whose content is View and whose data is SparseArray. * Author: msdx (645079761@qq.com) * Time: 14-8-18 */public abstract class AbstractPagerSparseAdapter <T> extends AbstractViewPagerAdapter {protected SparseArray <T> mData; public AbstractPagerSparseAdapter (SparseArray <T> data) {mData = data ;}@ Override public int getCount () {return mData. size ();} public abstract View newView (int position); public T getItem (int position) {return mData. valueAt (position );}}

In use, you only need to inherit and implement the newView (int position) method.


Android library development, want to make an encapsulated library for others to call, others can use this library to achieve the corresponding work

This can be.
Previously, I wrote a Java project that communicates with the server. Then I made a jar package for the android project to call.

Encapsulate the package into a jar package.

The principle of Sidebar implementation in android development. I Want To encapsulate one by myself and don't want to use another person's

It is easy to put a ListView in a FrameLayout file. If only a few icons are fixed, Several icons are directly written.

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.