Android listview gridview gallery adapter Performance Optimization

Source: Internet
Author: User

There are too many code controls in the past. Due to the project time issue, we seldom take into account the optimization details. Today we will briefly discuss the optimization effect of the listview control when using the adapter: good direct texture: Let's first look at the comparison effect:

When converview is not determined to be empty, that is, the time consumed for loading is:

When the convertview is null, Cai reloads the data, that is, the cached data is used:

The following figure shows the time when the static component is used, that is, the time when the findbyid () is put into the temporary memory cache.

It can be seen from the necessity of code optimization:

Well, I am not talking nonsense. I will directly post the core code, hoping to provide a better reference for some people who love Android:

Package com. geili. VANCL. activity;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. Button;
Import Android. widget. gridview;
Import Android. widget. imageview;
Import Android. widget. textview;

Import com. geili. VANCL. R;
Import com. geili. VANCL. util. mylogger;

/**
*
* @ Author jankey
*
*/
Public class catedetailactivity extends activity {

Private Static final mylogger logger = mylogger. getlogger ("[==== homeactivity ===]");

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. catedetail );
Findviewbyid ();
}

Private void findviewbyid (){
Topbar ();
Setcatedetaildatas ();
}

Private button mmen, mfemale;
Private gridview mcatedetaillist;
Private void setcatedetaildatas (){
Mmen = (button) findviewbyid (R. Id. men );
Mfemale = (button) findviewbyid (R. Id. Female );
Mcatedetaillist = (gridview) findviewbyid (R. Id. catedetaillist );
Mmen. setonclicklistener (listener );
Mfemale. setonclicklistener (listener );
Mcatedetaillist. setadapter (New catedetailadapter (catedetailactivity. This ));
}

Private onclicklistener listener = new onclicklistener (){
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. Men:

Break;
Case R. Id. FEMALE:

Break;
}
}
};

/* Public class catedetailadapter extends baseadapter {

Private context mcontext;

Public catedetailadapter (context c ){
This. mcontext = C;
}

@ Override
Public int getcount (){
Return 100000;
}

@ Override
Public object getitem (INT position ){
Return NULL;
}

@ Override
Public long getitemid (INT position ){
Return 0;
}

Private imageview mcatedetailimg, mstar;
Private textview mcatedetailprice, msalestotal;
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Long startdate = system. nanotime ();
Layoutinflater minflate = layoutinflater. From (mcontext );
// If (convertview = NULL ){
Convertview = minflate. Inflate (R. layout. catedetail_item, null );
//}
Mcatedetailimg = (imageview) convertview. findviewbyid (R. Id. catedetailimg );
Mstar = (imageview) convertview. findviewbyid (R. Id. Star );
Mcatedetailprice = (textview) convertview. findviewbyid (R. Id. catedetailprice );
Msalestotal = (textview) convertview. findviewbyid (R. Id. salestotal );
Mcatedetailimg. setimageresource (R. drawable. Splash );
Mstar. setimageresource (R. drawable. Speaker );
Mcatedetailprice. settext ("56.00 ");
Msalestotal. settext ("299393 ");
Long enddate = system. nanotime ();
Long resultvalue = (enddate-startdate)/1000l;
Logger. V ("resultvalue Date: ===========================>" + resultvalue );
If (New catedetailadapter (mcontext). getcount () <100 ){
If (resultvalue <1000l ){

}
}
Return convertview;
}
}*/

/* Public class catedetailadapter extends baseadapter {

Private context mcontext;

Public catedetailadapter (context c ){
This. mcontext = C;
}

@ Override
Public int getcount (){
Return 100000;
}

@ Override
Public object getitem (INT position ){
Return NULL;
}

@ Override
Public long getitemid (INT position ){
Return 0;
}

Private imageview mcatedetailimg, mstar;
Private textview mcatedetailprice, msalestotal;
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Long startdate = system. nanotime ();
Layoutinflater minflate = layoutinflater. From (mcontext );
If (convertview = NULL ){
Convertview = minflate. Inflate (R. layout. catedetail_item, null );
}
Mcatedetailimg = (imageview) convertview. findviewbyid (R. Id. catedetailimg );
Mstar = (imageview) convertview. findviewbyid (R. Id. Star );
Mcatedetailprice = (textview) convertview. findviewbyid (R. Id. catedetailprice );
Msalestotal = (textview) convertview. findviewbyid (R. Id. salestotal );
Mcatedetailimg. setimageresource (R. drawable. Splash );
Mstar. setimageresource (R. drawable. Speaker );
Mcatedetailprice. settext ("56.00 ");
Msalestotal. settext ("299393 ");
Long enddate = system. nanotime ();
Long resultvalue = (enddate-startdate)/1000l;
Logger. V ("resultvalue Date: ===========================>" + resultvalue );
If (New catedetailadapter (mcontext). getcount () <100 ){
If (resultvalue <1000l ){

}
}
Return convertview;
}
}*/

Public class catedetailadapter extends baseadapter {

Private context mcontext;

Public catedetailadapter (context c ){
This. mcontext = C;
}

@ Override
Public int getcount (){
Return 100000;
}

@ Override
Public object getitem (INT position ){
Return NULL;
}

@ Override
Public long getitemid (INT position ){
Return 0;
}

@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Long startdate = system. nanotime ();
Viewholder mviewholder;
Layoutinflater minflate = layoutinflater. From (mcontext );
If (convertview = NULL ){
Convertview = minflate. Inflate (R. layout. catedetail_item, null );
Mviewholder = new viewholder ();
Mviewholder. mcatedetailimg = (imageview) convertview. findviewbyid (R. Id. catedetailimg );
Mviewholder. mstar = (imageview) convertview. findviewbyid (R. Id. Star );
Mviewholder. mcatedetailprice = (textview) convertview. findviewbyid (R. Id. catedetailprice );
Mviewholder. msalestotal = (textview) convertview. findviewbyid (R. Id. salestotal );
Convertview. settag (mviewholder );
} Else {
Mviewholder = (viewholder) convertview. gettag ();
}
Mviewholder. mcatedetailimg. setimageresource (R. drawable. Splash );
Mviewholder. mstar. setimageresource (R. drawable. Speaker );
Mviewholder. mcatedetailprice. settext ("56.00 ");
Mviewholder. msalestotal. settext ("299393 ");
Long enddate = system. nanotime ();
Long resultvalue = (enddate-startdate)/1000l;
Logger. V ("resultvalue Date: ===========================>" + resultvalue );
/* If (New catedetailadapter (mcontext). getcount () <100 ){
If (resultvalue <1000l ){

}
}*/
Return convertview;
}
}

Static class viewholder {
Imageview mcatedetailimg, mstar;
Textview mcatedetailprice, msalestotal;
}

Private button msearch;
Private textview mproducename;
Private void topbar (){
Msearch = (button) findviewbyid (R. Id. Search );
Mproducename = (textview) findviewbyid (R. Id. producename );
Mproducename. settext (gettoptitle ());
Msearch. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Finish ();
}
});
}

Private string gettoptitle (){
String STR = NULL;
If (STR = NULL ){
STR = "t laxity ";
}
Return STR;
}
}

In addition, if you are interested, you can use some data to test the cached results. In comparison, I use five columns of data in the gridview, and the image is 50 kb, the number of entries is 100 kb. The test is as follows:

Do not use static:

The following figure shows the effect of layer optimization through static mode: by comparing them with each other, we can see that the effect of static layer optimization is significant for several months:

Continue to add: the number of KB, the size of 60 kb, and the display of 10 columns are initialized after layoutinflate:

Initialized after layoutinflater is used:

Looking at the details above, it will be effective when you use big data.

Related Article

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.