Use of the Android ListView control

Source: Internet
Author: User

How to make the following effect:

First, look at the layout of the main interface Activity_main.xml:toolbar + ListView

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.ahcc08.ahcc08.listviewdemo.MainActivity">    <Android.support.design.widget.AppBarLayoutAndroid:layout_height= "Wrap_content"Android:layout_width= "Match_parent"Android:theme= "@style/apptheme.appbaroverlay">        <Android.support.v7.widget.ToolbarAndroid:id= "@+id/toolbar"Android:layout_width= "Match_parent"Android:layout_height= "? Attr/actionbarsize"Android:background= "? Attr/colorprimary"App:popuptheme= "@style/apptheme.popupoverlay" />    </Android.support.design.widget.AppBarLayout>    <ListViewAndroid:id= "@+id/listview"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1">    </ListView></LinearLayout>

Second, create a new item layout to display the ListView: List_item.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <RelativelayoutAndroid:id= "@+id/ral_p1"Android:layout_width= "Match_parent"Android:layout_height= "60DP" >        <ImageViewAndroid:id= "@+id/iv_p1"Android:layout_width= "35DP"Android:layout_height= "29DP"android:layout_centervertical= "true"Android:layout_marginleft= "3DP"android:src= "@drawable/hot" />        <TextViewAndroid:id= "@+id/title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"Android:layout_marginleft= "13DP"Android:layout_torightof= "@+id/iv_p1"Android:text= "ListItem"android:textsize= "16SP" />        <ImageViewAndroid:layout_width= "12DP"Android:layout_height= "14.5DP"Android:layout_alignparentright= "true"android:layout_centervertical= "true"Android:layout_marginright= "15DP"android:src= "@drawable/info_go" />        <ViewAndroid:layout_width= "Match_parent"Android:layout_height= "1DP"Android:layout_alignparentbottom= "true"Android:background= "#ff00c0ff" />    </Relativelayout></LinearLayout>

Third, the main interface implementation: Mainactivity.java

 PackageCom.ahcc08.ahcc08.listviewdemo;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.Toolbar;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ListView;ImportAndroid.widget.Toast;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsappcompatactivity {List<String> myList =NewArraylist<string>();    ListView ListView;    Mylistadapter adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Toolbar Toolbar=(Toolbar) Findviewbyid (R.id.toolbar);        Setsupportactionbar (toolbar);        Initlist (); //Get the ListView controlListView =(ListView) Findviewbyid (R.id.listview); //Create a new adapteradapter =NewMylistadapter (mainactivity. This, myList);        Listview.setadapter (adapter); Listview.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {String text=Mylist.get (position); Toast.maketext (mainactivity. This, text, Toast.length_short). Show ();    }        }); }    Private voidinitlist () {mylist.clear ();  for(inti = 0; I < 10; i++) {Mylist.add ("Item" +i); }    }}

Four, the implementation of the adapter: Mylistadapter.java

 PackageCom.ahcc08.ahcc08.listviewdemo;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.TextView;Importjava.util.List; Public classMylistadapterextendsbaseadapter {layoutinflater inflater=NULL; List<String>Mlistdata;  PublicMylistadapter (context context, list<string>mlistdata) {        Super(); Inflater=Layoutinflater.from (context);  This. Mlistdata =Mlistdata; } @Override Public intGetCount () {returnmlistdata.size (); } @Override Public LongGetitemid (intposition) {        returnposition; } @Override PublicObject GetItem (intposition) {        returnMlistdata.get (position); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder VH=NULL; if(Convertview = =NULL) {Convertview= Inflater.inflate (R.layout.list_item,parent,false); VH=NewViewholder (); Vh.tv_title=(TextView) Convertview.findviewbyid (r.id.title);        Convertview.settag (VH); }Else{VH=(Viewholder) Convertview.gettag (); } String sinfo=(String) getItem (position);        Vh.tv_title.setText (Sinfo); returnConvertview; }     Public voidAddAll (list<string> List,Booleanisclear) {        if(isclear) {mlistdata.clear ();        Mlistdata.addall (list); }Else{mlistdata.addall (list);    } notifydatasetchanged (); }     Public voidAdd (String t) {Mlistdata.add (t);    Notifydatasetchanged (); }     Public voidRemove (String t) {mlistdata.remove (t);    Notifydatasetchanged (); }     Public voidClear () {mlistdata.clear ();    Notifydatasetchanged (); }    classviewholder{TextView Tv_title; }}

The code is quite simple.

Use of the Android ListView control

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.