Yesterday to xutils a simple introduction to the whole, today we on the code, the real gun to see how to see how fast and convenient to integrate xutils to everyone's project. There are four components in xutils that we can use, namely Viewutils, Httputils, Bitmaputils, and Dbutils. If you do not read my previous article first, then please move to the past to understand the whole, and then look back to this article, I believe you have more experience.
The following are some examples of how these components are used in turn.
Viewutils the IOC (control inversion) framework in Android, you can complete the binding and event binding of the UI using annotations entirely. Simply put, Viewutils's function is to do this, but it can be said that such a function can greatly simplify our code. Let's take a look at the specific code, and by the way, the difference between the ID and the Findviewbyid in the manner of annotations.
@ViewInject (r.id.btn) private Span style= "color: #000000;" > Button btn; @ViewInject (r.id.img) private ImageView img; @ViewInject (r.id.list) private ListView list; @Override protected void OnCreate (Bundle savedinstancestate) { super .oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); Viewutils.inject ( this );}
@Override protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_second); = (Button) Findviewbyid (R.ID.BTN); = (ImageView) Findviewbyid (r.id.img); = (ListView) Findviewbyid (r.id.list); }
If you have a large number of controls in your project, imagine that big lump in your code is really unbearable. With Xutils's simple annotations, you can easily get rid of the endless bad taste code.
Note: When using annotations to bind controls, be sure to call Viewutils.inject (this) in OnCreate;
Let's compare the differences between event bindings in Android.
@OnClick ({r.id.btn, r.id.img}) Public void Clickmethod (View v) { toast.maketext (secondactivity. This, "you clicked Button!" , Toast.length_short). Show (); } @OnItemClick (r.id.list) publicvoidint position,long ID) { toast.maketext (secondactivity. This, "Position--->" + position, toast.length_short). Show ();
Btn.setonclicklistener ( This); List.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {toast.maketext (secondactivity. This, "Position--->" +position, Toast.length_short). Show (); } }); @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn:toast.maketext (secondactivity. This, "You clicked Button!", Toast.length_short). Show (); Break; default: Break; } }
The listener event that originally binds a button either uses an ugly inner class, or the activity implements Onclicklistener, in the replication OnClick method to go by ID. As long as the xutils can realize the function of listening events through a simple annotation, and can implement multiple controls to share a listening method. At the same time, Xutils provides the onclick, Onitemclick, Onlongclick and other 15 kinds of event monitoring annotations.
Note: When using annotations to listen for events, the Listener method name is custom, but make sure that the method's access modifier is public, and that the parameters of the method are consistent with the original listener parameters of the Android, not only the parameter types, but also the order of the parameters.
Second, the Bitmaputils load network or local bitmap time without worrying about the phenomenon of oom, management bitmap memory using the LRU algorithm, but also to avoid the list of sliding in the process of the phenomenon of image dislocation. When loading a network picture, you can also configure the number of running threads, cache path, etc... With the various constructors of bitmaputils, it is easy to create the local cache path and cache size, as well as the size of the memory cache.
Bitmaputils Utils =NewBitmaputils ( This); Bitmapdisplayconfig Config=NewBitmapdisplayconfig ( This); Config.setloadingdrawable (Getresources (). getdrawable (r.drawable.loading)); Config.setloadfaileddrawable (Getresources (). getdrawable (r.drawable.failed)); Config.setimageloadcallback (NewImageloadcallback () {@Override Public voidloadfailed (ImageView ImageView, drawable drawable) {} @Override Public voidloadcompleted (ImageView ImageView, drawable drawable, Bitmapdisplayconfig config) {} }); Config.setbitmapmaxwidth (480); Config.setbitmapmaxheight (720);//Utils.display (IMG, "http://img1.gtimg.com/news/pics/hv1/63/26/1451/94357968.jpg");Utils.display (IMG, "http://img1.gtimg.com/news/pics/hv1/63/26/1451/94357968.jpg", config);
Bitmaputils when used to load a network picture, you can configure the size of the loaded picture, load the successful and failed callbacks, and the configuration of the picture during loading. You can also choose not to configure.
// support loading local images // When you use a container such as a listView to display a picture, the Pauseonscrolllistener controls the slide and pauses loading of the picture during the quick slide listview.setonscrolllistener (new false true ) ); Listview.setonscrolllistener (newfalsetrue), Customlistener);
The remaining two components: Dbutils and Httputils
Transferred from: http://my.oschina.net/jack1900/blog/173526
Android-xutils Framework Introduction (II)