About the optimization of the ListView

Source: Internet
Author: User

The ListView is a very common component in Android development. The ListView is required for both content and settings, as long as it is non-single content in the same format.

There are a lot of Kat in the ListView, the most simple is simpleadapter. The content that will be displayed corresponds to component one by one in the layout.

Simpleadapter is an adapter that, through its developers, sets the corresponding relationship between the components in the ListView's item layout and the data before it can form an item containing the ImageView and TextView.

For this usage, refer to Foocoder's blog: http://www.cnblogs.com/noTice520/archive/2011/12/05/2276379.html

Sometimes, however, our needs are more complex. For example, we may need to have a button in item. If, continue with the above method, button is no way to get the focus, our click will only trigger the ListView item's Click event, will not trigger the button click event. In addition, when we need to display a very large number of item, the ListView will try to display all of the item at once, which will put a considerable burden on the system and may even lead to oom. At this point, we need to use baseadapter to generate the ListView. When we build our own adapter to inherit Baseadapter, we need to implement several methods.

@Override Public intGetCount () {returnadapterobjectlist.size (); } @Override PublicObject GetItem (intposition) {            return NULL; } @Override Public LongGetitemid (intposition) {            return0; } @Override PublicView GetView (intPosition, View Convertview, ViewGroup parent) {
}

For the use of baseadapter, we can also refer to the link above http://www.cnblogs.com/noTice520/archive/2011/12/05/2276379.html

However, this is more troublesome. Because a ListView display code will be very long, if you want to modify the layout, it is also very troublesome. Principle since there is already large capitalization here, I am here to do an immature parent, will often need to change the code and infrequently changed code separate, easy to use. The code is as follows:

ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ListView;Importjava.util.List;/*** Created by Administrator on 2015/4/4.*/ Public Abstract classEasylistviewextendsListView { PublicEasylistview (Context context) {Super(context); }     PublicEasylistview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicEasylistview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr);    } Context Mcontext; intMitemlayoutid;    Easylistview Msonlistview; /*** Get the ID of the layout file in the ListView item *@returnMitemlayoutid*/    protected Abstract intGetlistviewitemlayoutid (); /*** Get the ID of the component in item *@paramConvertview Item is located in the view*/    protected Abstract voidGetviewid (View convertview); /*** Update the components in item*/    protected Abstract voidUpdateview (Object dataObject); /*** Update the ListView interface *@paramContext Contexts *@paramInputdatalist getting Information*/     Public Abstract voidUpdatelistview (context context, list<?>inputdatalist);
protectedMyadapter Getadapter (Context context,list<?>dataList, Easylistview Sonlistview) {Mcontext=context; Msonlistview=Sonlistview; Mitemlayoutid=Msonlistview.getlistviewitemlayoutid (); Myadapter Myadapter=Newmyadapter (context, dataList); returnMyadapter; }
//List of adapters protected classMyadapterextendsBaseadapter {Context adaptercontext; PrivateLayoutinflater Adapterinflater;//get a Layoutinfalter object to import the layout PrivateList<?>adapterobjectlist; PublicMyadapter (Context mycontext, list<?>inputdatalist) {Adaptercontext=Mycontext; Adapterinflater=Layoutinflater.from (mycontext); Adapterobjectlist=inputdatalist; } @Override Public intGetCount () {returnadapterobjectlist.size (); } @Override PublicObject GetItem (intposition) { return NULL; } @Override Public LongGetitemid (intposition) { return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder viewholder; if(Convertview = =NULL){ //introduce the layout of each item in the listConvertview = Adapterinflater.inflate (Mitemlayoutid,NULL); Viewholder=NewViewholder (Adaptercontext); Viewholder.viewgetviewid (Convertview); Convertview.settag (Viewholder); } Else{Viewholder=(Viewholder) Convertview.gettag (); } viewholder.viewupdateview (Adapterobjectlist.get (position)); returnConvertview; } Public Final classViewholder {Context viewcontext; PublicViewholder (Context context) {ViewContext=context; } Public voidViewgetviewid (View convertview) {//connecting a declared component to a layoutMsonlistview.getviewid (Convertview); } Public voidViewupdateview (Object adapterobject) {//assign a value to a specific viewMsonlistview.updateview (Adapterobject); } } }}

Basically copy Foocoder big method, do not repeat. It should be explained that this is an abstract class, when we want to create a new ListView, we only need to define a class ourselves, inherit it, and then follow a certain rules to implement the method. Examples of use are as follows:

ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.widget.TextView;ImportCOM.MEMEDA.LSY.TOMATOFIGHT3.R;ImportCom.memeda.lsy.tomatofight3.data.RecordingData;Importjava.util.List;/*** Created by Administrator on 2015/4/4.*/ Public classDownloadinglistviewextendsEasylistview { PublicDownloadinglistview (Context context) {Super(context); }     PublicDownloadinglistview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicDownloadinglistview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); }
TextView Titletextview, Pathtextview; @Overrideprotected intGetlistviewitemlayoutid () {returnr.layout.download_listview_item_layout; } @Overrideprotected voidGetviewid (View convertview) {Titletextview=(TextView) Convertview.findviewbyid (r.id.downloadamrtitle); Pathtextview=(TextView) Convertview.findviewbyid (R.id.downloadamrauthor); } @Overrideprotected voidUpdateview (Object dataObject) {recordingdata recordingdata=(Recordingdata) dataObject; Titletextview.settext (Recordingdata.getrecordingname ()); Pathtextview.settext (""+recordingdata.getusername ()); } @Override Public voidUpdatelistview (context context, list<?>inputdatalist) {Myadapter Myadapter= Getadapter (Context, inputdatalist, This); Setadapter (Myadapter); } }

Use the following steps:

First, declare the components in item.

TextView Titletextview, Pathtextview;

Second, get the layout file (XML) of the ListView item.

@Override     protected int Getlistviewitemlayoutid () {        return  r.layout.download_listview_item_layout;    }

Third, get an instance of the component in item.

@Override     protected void Getviewid (View convertview) {        = (TextView) Convertview.findviewbyid (r.id.downloadamrtitle);        = (TextView) Convertview.findviewbyid (R.id.downloadamrauthor);    }

Iv. setting the corresponding relationship between the data and the component display when the ListView is generated/updated.

@Override     protected void Updateview (Object dataObject) {        = (recordingdata) dataObject;        Titletextview.settext (Recordingdata.getrecordingname ());        Pathtextview.settext ("" +Recordingdata.getusername ());    }

V. Implement the Update method of the ListView.

@Override      Public void Updatelistview (context context, list<?> inputdatalist) {        this);        Setadapter (myadapter);    

VI. Use it in activity, like a regular listview

Internetrecordlistview.updatelistview (this, recordingdatas);

Above.

 

  

About the optimization of the ListView

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.