ListView Learning (with pictures)

Source: Internet
Author: User

ListView is one of the most important components in wirelessly, let's learn it together! First, the legend download code


Second, the steps are as follows: 1. First we need to build two XML files

(1) Create a ListView in Activity_main.xml

<relativelayout xmlns:android="Http://" xmlns:tools= ""android:layout_width="Match_ Parent "android:layout_height="match_parent "tools:context=". Mainactivity " >                    <ListViewandroid:id="@+id/listview"android:layout_width="Match_ Parent "android:layout_height=" Wrap_content " >                         </ListView></relativelayout>      

(2) Create a ImageView and a TestView component in Fruit_item

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = ""  android:layout_width  =" match_parent " android:layout_height  =" wrap_content " android:orientation  =;     <imageview  android:id
      = "@+id/ivfruit"  android:layout_width  =< Span class= "Hljs-value" > "wrap_content"  android:layout_height  = "wrap_content"  android:src  =  "@drawable/apple_pic" />     <TextViewandroid:id= "@+id/tvfruit"android:layout_gravity=" Center "android:layout_margin=" 10DP "android:layout_width=" Wrap_content "  android:layout_height="Wrap_content"android:text="Apple" />                                                 </linearlayout>
Code in 2.Java (analysis)

(1) We have to prepare the data source, the data source here refers to some of the images and text information we want to use, we put it into the list, easy to use
(2) Create adapter adapter Fruitadapter adapter=new Fruitadapter (current text, Fruit_item layout,
(3) Binding Adapterview and Adapter Note:
Toast.maketext (Mainactivity.this, Fruit.getname ()
Toast.length_short). Show (); This is a click event that displays the name of the current column of fruit, using a spin to display the name of the fruit
Fruitadapter is a custom adapter

The code is as follows:
Packagecom. Example. UI_listview01;Import Java. Util. ArrayList;Import Java. Util. List;Import Android. App. Activity;Import Android. OS. Bundle;Import Android. View. Menu;Import Android. View. View;Import Android. Widgets. Adapterview;Import Android. Widgets. Toast;Import Android. Widgets. Adapterview. Onitemclicklistener;Import Android. Widgets. ListView;Importcom. Example. UI_listview01. Adpater. Fruitadapter;Importcom. Example. UI_listview01. Model. Fruit;public class Mainactivity extends Activity {private ListView lvfruits;//1.Prepare the data source private list<fruit> fruitlist = new Arraylist<fruit> ();@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);//1.Prepare the data source Initfruits ();Lvfruits = (ListView) Findviewbyid (R. ID. Lvfruits);//2.Create Adapter Fruitadapter adapter=new Fruitadapter (This, R. Layout. Fruit_item,fruitlist);//3.Binds the association between Adapterview and adapter Lvfruits. Setadapter(adapter);Lvfruits. Setonitemclicklistener(New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> adapterview, Vie W view, int position, long id) {Fruit fruit= fruitlist. Get(position);Toast. Maketext(mainactivity. this, fruit. GetName(), Toast. LENGTH_short). Show();}        });} private void Initfruits () {Fruit Apple = new Fruit ("Apple"R. drawable. Apple_pic);Fruitlist. Add(apple);Fruit banana = new Fruit ("Banana"R. drawable. Banana_pic);Fruitlist. Add(banana);Fruit orange = new Fruit ("Orange"R. drawable. Orange_pic);Fruitlist. Add(orange);Fruit watermelon = new Fruit ("Watermelon"R. drawable. Watermelon_pic);Fruitlist. Add(watermelon);Fruit pear = new Fruit ("Pear"R. drawable. Pear_pic);Fruitlist. Add(pear);Fruit grape = new Fruit ("Grape"R. drawable. Grape_pic);Fruitlist. Add(grape);Fruit pineapple = new Fruit ("Pineapple"R. drawable. Pineapple_pic);Fruitlist. Add(pineapple);Fruit Strawberry = new Fruit ("Strawberry"R. drawable. Strawberry_pic);Fruitlist. Add(Strawberry);Fruit cherry = new Fruit ("Cherry"R. drawable. Cherry_pic);Fruitlist. Add(Cherry);Fruit mango = new Fruit ("Mango"R. drawable. Mango_pic);Fruitlist. Add(Mango);}}

(1) Fruitadapter method inheritance Arrayadapter need to override GetView method
(2) GetView () This method is called when each subkey is scrolled into the screen GetView (current position, View Convertview,
ViewGroup parent)

Note: The layout is reloaded once each time in the Fruitadapter GetView () method, which becomes a performance bottleneck when the ListView is rolling fast.

The code is as follows:
Packagecom. Example. UI_listview01. Adpater;Import Java. Util. List;Import Android. Content. Context;Import Android. View. Layoutinflater;Import Android. View. View;Import Android. View. ViewGroup;Import Android. Widgets. Arrayadapter;Import Android. Widgets. Baseadapter;Import Android. Widgets. ImageView;Import Android. Widgets. TextView;Importcom. Example. UI_listview01. R;Importcom. Example. UI_listview01. Model. Fruit;public class Fruitadapter extends arrayadapter<fruit> {private int resourceId;Public Fruitadapter (context context, int textviewresourceid,list<fruit> data) {Super (context, Textviewresour CeId, data);Resourceid=textviewresourceid;} @Override public View getView (int position, view Convertview, ViewGroup parent) {Fruit Fruit = GetItem (pos ition);View View=layoutinflater. from(GetContext ()). Inflate(ResourceId, NULL);ImageView ivfruit= (ImageView) view. Findviewbyid(R. ID. Ivfruit);TextView tvfruit= (TextView) view. Findviewbyid(R. ID. Tvfruit);Ivfruit. Setimageresource(Fruit. Getimageid());Tvfruit. SetText(Fruit. GetName());Return view;}}
The code is as follows:
 PackageCom.bzu.gxs.domain; Public  class Fruit {    PrivateString name;Private intImageId;/**     *      */     Public Fruit(String name,intIMAGEID) {Super(); This. name=name; This. Imageid=imageid; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getimageid() {returnImageId; } Public void Setimageid(intIMAGEID) { This. imageId = ImageId; }}
Iii. Summary

ListView Learning (with pictures)

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: 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.