Android encapsulates a View module
Android
The uidesign is visible, that is, the space available, which is very convenient when designing the UI. We should also try to reuse the UI to improve efficiency. If the entire page is the same, or only the list content
Different pages are displayed. We should use the same page instead of configuring multiple pages. But what should we do if only a part of the page has the same content and we cannot share the whole page?
We can encapsulate this module, as long as it is introduced in the place where it is used. Let's look at a small example of gallery encapsulation.
1. classes that encapsulate Gallery:
Package com. d_gallerypackage;
Import java. util. arraylist;
Import java. util. List;
Import Android. content. context;
Import Android. content. intent;
Import Android. util. attributeset;
Import Android. View. gravity;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. View. onclicklistener;
Import Android. View. animation. animation;
Import Android. View. animation. animationutils;
Import Android. widget. adapterview;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageview;
Import Android. widget. linearlayout;
Import Android. widget. adapterview. onitemclicklistener;
Public class gallerylayout extends linearlayout implements onclicklistener, onitemclicklistener {
Private context mcontext;
Private imageview pre, next;
Private gallery mgallery;
Private int fill = viewgroup. layoutparams. fill_parent;
Private int wrap_content = viewgroup. layoutparams. wrap_content;
Private list <integer> myimage = new arraylist <integer> ();
Public gallerylayout (context, attributeset attrs ){
Super (context, attrs );
Mcontext = context;
}
Public void updateui (){
Myimage. Add (R. drawable. coupons_new );
Myimage. Add (R. drawable. profile_ad_icon_new );
Myimage. Add (R. drawable. profile_menu_icon_new );
Myimage. Add (R. drawable. save_icon_new );
Myimage. Add (R. drawable. video_icon_new );
Pre = new imageview (mcontext );
Pre. setimagedrawable (getresources (). getdrawable (R. drawable. map_point_back ));
Next = new imageview (mcontext );
Next. setimagedrawable (getresources (). getdrawable (R. drawable. map_point_forward ));
Mgallery = new gallery (mcontext, null );
Imageadapter adapter = new imageadapter (mcontext );
Mgallery. setadapter (adapter );
Mgallery. setlayoutparams (New layoutparams (fill, wrap_content, 1 ));
Setgalleryselection (mgallery );
Pre. setonclicklistener (this );
Next. setonclicklistener (this );
Mgallery. setonitemclicklistener (this );
Setgravity (gravity. center );
Addview (pre );
Addview (mgallery );
Addview (next );
}
Private void setgalleryselection (GALLERY Gallery ){
If (myimage. Size ()> = 2 ){
Gallery. setselection (1 );
}
}
@ Override
Public void onclick (view v ){
If (V = pre ){
Int id = (INT) mgallery. getselecteditemid ();
If (ID> 0 ){
Animation animation = animationutils. loadanimation (mcontext, R. anim. push_left_in );
Mgallery. setanimation (animation );
Mgallery. setanimationcacheenabled (true );
Mgallery. setselection (-- id, true );
}
} Else if (V = NEXT ){
Int id = (INT) mgallery. getselecteditemid ();
If (ID <(myimage. Size ()-1 )){
Mgallery. setselection (++ ID, true );
}
}
}
@ Override
Public void onitemclick (adapterview <?> Parent, view, int position,
Long ID ){
New
Alertdialog. Builder (mcontext). setmessage (string. valueof (position). settitle ("Gallery
Click Event "). setpositivebutton (" = OK = ", new
Dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
}
}). Show ();
}
Public class imageadapter extends baseadapter {
Private context mycontext;
Public imageadapter (context c)
{
Mycontext = C;
}
@ Override
Public int getcount (){
Return myimage. Size ();
}
@ Override
Public object getitem (INT position ){
Return position;
}
@ Override
Public long getitemid (INT position ){
Return position;
}
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Imageview TV = new imageview (mycontext );
TV. setimagedrawable (getresources (). getdrawable (myimage. Get (position )));
TV. setpadding (20, 10, 29, 10 );
TV. setlayoutparams (new gallery. layoutparams (wrap_content, wrap_content ));
Return TV;
}
}
}
2. Introduce In the UI:
<Com. d_gallerypackage.gallerylayout
Android: layout_gravity = "center"
Android: clickable = "false"
Android: focusable = "false"
Android: l
Android: Id = "@ + ID/gallerylayout"
Android: layout_height = "75dip"
Android: layout_width = "fill_parent"
Android: Background = "# fff"/>
3. Call in the Code (where gallery is to be used, and with the UI:
Gallerylayout = (gallerylayout) findviewbyid (R. Id. gallerylayout );
Gallerylayout. updateui ();