The general use of pageadapter need to implement GetCount, Isviewfromobject, Instantiateitem, Destroyitem and other default functions, in fact, you can extract the common code into the parent class.
/**
* Created by Xiaoxuan948 on 2015/10/27.
* Desc:
* Hint: Set data to adapter first, then bind adapter Setadapter Call Instantiateitem method
*/
Public classabsunitybasepageadapter<T>extendsPageradapterImplementsInotifyadapterdatasetchange {
private static FinalUtilslogLG= Utilslog.GetLogger(Absunitybasepageadapter.class. Getsimplename ());
PrivateLayoutinflaterLayoutinflater;
Privateiadapterdatamodifyhelper<T>Iadapteratamodifyhelper;
PublicAbsunitybasepageadapter () {
This.Iadapteratamodifyhelper=NewDatamodifyhelper<> ();
}
Publiciadapterdatamodifyhelper<T>Getdatamodifyhelper() {
This.Iadapteratamodifyhelper. Setnotifyadapterdatasetchange ( This);
returnIadapteratamodifyhelper;
}
@Override
Public voidNotifyadapter() {
LG. E ("Notifyadapter with DataSize:"+ This.Iadapteratamodifyhelper. getdataresources (). Size ());
Notifydatasetchanged ();
}
/*pageradapter Default must implement method */
@Override
Public BooleanIsviewfromobject(View view, Object object) {
LG. E ("Isviewfromobject:"+ (view = = object?)"true":"false"));
returnView = = Object;
}
@Override
public intGetCount() {
returnIadapteratamodifyhelper. getdataresources (). Size ();
}
@Override
PublicObjectInstantiateitem(ViewGroup container, intPosition) {
if(Layoutinflater==NULL) {
Layoutinflater= Layoutinflater. from(Container.getcontext ());
}
if(Position >=Iadapteratamodifyhelper. getdataresources (). Size ()) {
LG. E ("Fix Position:instantiateitem failed for position:"+ Position +"Out of size:"+Iadapteratamodifyhelper. getdataresources (). Size ());
Position = position% getdatamodifyhelper (). Getdataresources (). Size ();
}
LG. E ("Generateitemview for Position:"+ position);
View Itemview = Generateitemview (Iadapteratamodifyhelper. Getdataresources (). Get (position), Layoutinflater, Position;
Container.addview (Itemview);
returnItemview;
}
@Override
//Do not call will error
Public voidDestroyitem(ViewGroup container, intPosition, Object object) {
LG. E ("Destroyitem:"+ position);
Container.removeview ((View) object);
}
/* Custom Abstract method: Build the View object based on position */
protectedViewGenerateitemview(TItementity, Layoutinflater Layoutinflater, intPosition) {
if(itementityinstanceofView) {
return(View)Iadapteratamodifyhelper. Getdataresources (). Get (position);
}Else{
throw NewNullPointerException ("Dataresource Type is not view,so child class must Override");
}
}
}
Among them, iadapteratamodifyhelper in the encapsulation of data additions and deletions and other operations, and has been internally processed null pointer exception. Instantiateitem China, the first step is to position the Generateitemview method to generate the View object and add it to the container by correcting the bounds of the array. In the Generateitemview implementation, the default implementation determines whether the current dataset is a collection of view, otherwise throws an exception, primarily to force the developer to rewrite Generateitemview to generate the View object based on Itementity.
The business layer is used as follows:
Adapter=NewAbsunitybasepageadapter<string> () {
@Override
protectedViewGenerateitemview(String itementity, Layoutinflater Layoutinflater, intPosition) {
LG. E ("Viewpagerlabbaseadapter Generateitemview with position:"+ position);
View Itemrootview = Layoutinflater.inflate (r.layout.simple_item_vertical, NULL);
Viewfindhelper Viewfindhelper =NewViewfindhelper (Itemrootview);
ImageView Imgitemcommon = Viewfindhelper.generateview (r.id.Imgitemcommon);
Imgitemcommon.setscaletype (Imageview.scaletype.Center_crop);
Glide. with(Layoutinflater.getcontext ()). Load (itementity). into (Imgitemcommon);
returnItemrootview;
}
};
Adapter. Getdatamodifyhelper (). Setdataresource (
"Http://tupian.enterdesk.com/2013/mxy/12/10/15/3.jpg",
"Http://pic1.nipic.com/2008-11-13/2008111384358912_2.jpg",
"Http://img4.imgtn.bdimg.com/it/u=1249974208,2325623016&fm=21&gp=0.jpg",
"Http://img0.imgtn.bdimg.com/it/u=1918423052,1540132432&fm=21&gp=0.jpg");
Vpactviewpager. Setadapter (Adapter);
From for notes (Wiz)
Definition of Android Adapter Pageradapter Universal Adapter