Android development skills-PagerAdapter implementation class encapsulation, pageradapter
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 (645079761@qq.com) * 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 view, Object object) {return view = object ;}@ 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;} public abstract View newView (int position); @ Override public void destroyItem (ViewGroup container, int position, Object object Object) {container. removeView (mViews. get (position);} public T getItem (int position) {return mData. get (position );}}
This class uses generics to support extension reuse. 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?
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.
How can classes be encapsulated?
1. The basic type can only be passed by value, and the encapsulation class corresponding to each basic type is passed by reference.
2. In terms of performance, the basic types in java are created on the stack, and all object types are created on the stack (Object references are created on the stack ). For example
Integer I = new Integer (10); where new Integer () is created on the stack, and its reference Integer I is on the stack. The appearance of encapsulation classes is to make it easier to use methods that are not available to basic types, such as valueOf () and toString. In addition, if you want to pass an int object reference instead of a value, you can only use the encapsulation class.
The Calling efficiency of allocating memory on the stack is much lower than that of allocating memory on the stack. Although it is highly efficient to allocate memory on the stack, there is a memory leakage problem in allocating memory on the stack. (This is basically a problem that mediocre programmers cannot solve...) java uses a very talented method to improve the efficiency of allocating memory on the heap. However, java is still slow. He is unlikely to reach the speed of c ++, although he has been promising that one day virtual opportunities will be as fast as machine code.
Reference: zhidao.baidu.com/question/24293668.html