Adapter of Android App performance improvement

Source: Internet
Author: User


Android app user swipe list, will callback adapter in the GetView method, here can not each item to create a view, so you need to start in the GetView to determine whether the second parameter view is empty.

If it is empty, you need to call the inflate () method to create a new view, and if not empty, use the second parameter view directly.


Well, if an interface has more than two view needs to switch, then the above performance improvement is not the correct way. You need to rewrite the Getitemviewtype method and the Getviewtypecount method in adapter,

This method will be recalled before GetView, and the Android system will return a different view in GetView based on the return value of the Getitemviewtype.


For example: If an interface has an edit mode and a non-editing mode, then the Getviewtypecount method in adapter needs to return 2 directly, indicating that there are two modes, while the Getitemviewtype

It is necessary to judge that the current pattern returns a different itemtype, so that when the GetView callback is used, the view (the second parameter) is based on the different type.


Special Note: The type returned in Getitemviewtype must be counted from 0, otherwise the group will be out of bounds of the exception!

Define two modes

Private final int normal_mode = 0;
Private final int edite_mode = 1;



@Override
public int Getviewtypecount () {
return 2;
}


@Override
public int Getitemviewtype (int position) {
if (Miseditemode) {
return edite_mode;
} else {
return normal_mode;
}
}


@Override
Public View getView (int index, View arg1, ViewGroup arg2) {
View view = null;
int itemType = Getitemviewtype (index);
if (arg1 = = null) {
if (ItemType = = Normal_mode) {
LOG.I ("zzzzzz/////", "non-editing mode is empty");
View = Layoutinflater.from (Mcontext). Inflate (R.layout.list_item_local_info, arg2, false);
} else if (ItemType = = Edite_mode) {
LOG.I ("zzzzzz/////", "edit mode is empty");
View = Layoutinflater.from (Mcontext). Inflate (R.layout.list_item_edite_local_info, arg2, false);
Initviewediteitem (view, E, index);

}
} else {
view = Arg1;
if (ItemType = = Normal_mode) {
LOG.I ("zzzzzz/////", "non-edit mode NOT NULL");
Initviewitem (view, E, index);
} else if (ItemType = = Edite_mode) {
LOG.I ("zzzzzz/////", "edit mode is not empty");
Initviewediteitem (view, E, index);
}
}

return view;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Adapter of Android App performance improvement

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.