How to rewrite Baseadapter and optimize ListView in Android

Source: Internet
Author: User
Tags static class

Background

For ListView, GridView, Gallery, spinner, and so on, it is their adapter, directly inherit from the interface class adapter, the use of baseadapter need to rewrite many methods, the most important of which is GetView, Because this involves issues such as ListView optimization, Baseadapter is somewhat different from other adapter, other adapter can set data directly in its constructor:

Simpleadapter adapter = new Simpleadapter (this, GetData (), R.layout.list_item, New string[]{"img", "title", "Info", Newint[]{r.id.img, R.id.title, R.id.info}});

But in Baseadapter you need to implement a class that inherits from Baseadapter, and rewrite many of the methods inside it:

Class Myadapter extends Baseadapter {private context;
        Public Myadapter {this.context = context; @Override public int GetCount () {//How many items are into the data set represented by this Adapter.
        (Number of entries in the data set represented in this adapter) return 0; @Override public Object getitem (int position) {//Get the ' data item associated with th e specified position in the data set.
        (Gets the data item that corresponds to the specified index in the dataset) return null; @Override public long getitemid (int position) {//Get the row ID associated with the ' s Pecified position in the list.
        (Take the row ID corresponding to the specified index in the list)//View more highlights in this column: Http://www.bianceng.cn/OS/extra/return 0; @Override public View getview (int position, View Convertview, ViewGroup parent) {//Ge T a View that displays the "Data at the" specified position in THe data set.
        return null; }
            
    }

There is no processing, it is not recommended to write.

If the amount of data is small, but if the list item data is very large, it will re-create the view every time, set the resources, seriously affect performance, so from the outset do not use this approach.

@Override public
        View getview (int position, view Convertview, ViewGroup parent) {
            View item = minflater.inflate (R . Layout.list_item, null);
            ImageView img = (imageview) Item.findviewbyid (r.id.img) 
            TextView title = (TextView) Item.findviewbyid (r.id.title);
            TextView info = (TextView) Item.findviewbyid (r.id.info);
            Img.setimageresource (r.drawable.ic_launcher);
            Title.settext ("Hello");
            Info.settext ("World");
                
            return item;
        }

ListView optimization

By caching Convertview, this method of caching Contentview can be used to determine if view is not present in the cache, and if there is already a view in the cache that can be used to improve performance.

Public View GetView (int position, View Convertview, ViewGroup parent) {
            if (Convertview = null)
            {
                Convertview = minflater.inflate (R.layout.list_item, null);
                
            ImageView img = (imageview) Convertview.findviewbyid (r.id.img) 
            TextView title = (TextView) Convertview.findviewbyid (r.id.title);
            TextView info = (TextView) Convertview.findviewbyid (r.id.info);
            Img.setimageresource (r.drawable.ic_launcher);
            Title.settext ("Hello");
            Info.settext ("World");
                
            return convertview;
        }

ListView Re-optimization

With Convertview+viewholder, Viewholder is a static class, and the key benefit of using Viewholder is caching the view that displays the data and speeding up the responsiveness of the UI.

When we judge Convertview = = NULL, if it is empty, it will assign value to Convertview according to the item layout (XML) of the design list. and generates a viewholder to bind the individual view controls within the Converview (those controls within the XML layout). Then use the Convertview Settag to set the Viewholder to the tag so that the system can draw the ListView from the tag when it draws the second time. (See the code below)

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.