Android Complex Custom ListView implementation

Source: Internet
Author: User

In the implementation of the ListView in Android is more difficult to understand the newcomer, I read a number of articles that can use the following ideas to let new people better understand (also do a good record, lest they forget later).

Refer to the blog: http://cinderella7.blog.51cto.com/7607653/1281696 (here with the idea of MVC to understand the ListView, personally think it is good)

http://blog.csdn.net/jueblog/article/details/11857281 (a complete implementation)

--------------------------------------------------------------------------Split Line-------------------------------------------- ---------------------

First, straighten out the whole idea.

The custom ListView contains three important concepts, which are understood in the form of MVC:

ListView is equivalent to V (view) in the MVC framework

Adapter equivalent to C (Controller) in the MVC framework

  The data source is equivalent to M (model) in the MVC framework

Two or one step-by-step implementation

1. Customize the layout of each item subkey in the ListView first

Listitem.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageViewAndroid:layout_width= "Fill_parent"Android:layout_height= "100DP"Android:id= "@+id/imageview"/>    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:textappearance= "? Android:attr/textappearancelarge"Android:text= "Large Text"Android:id= "@+id/textview"/></LinearLayout>

2. Define the main layout

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"        >    <ListViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/listview"/></LinearLayout>

3. Then, define the data source, view, adapter

    Private List// M    Private ListView ListView;  // V    Private Myadapter Myadapter;  // C

4. Get the data source

PrivateListGetData () {ArrayList<HashMap<String,Object>> arrayList =NewArraylist(); HashMap<String,Object> HashMap =NULL;  for(inti=0;i<10;i++) {HashMap=NewHashmap<string, object>(); Hashmap.put ("Index", integer.tostring (i+1)); Hashmap.put ("IMG", r.drawable.my); Arraylist.add (HASHMAP); //add to Data source        }        returnarrayList; }

5. Custom adapter, add data source to adapter

//Create a custom adapter    Private classMyadapterextendsbaseadapter{PrivateLayoutinflater Mlayoutinflater;
       PublicMyadapter (Context context) {Mlayoutinflater= Layoutinflater.from (context);//Dynamic Layout Mapping} @Override Public intGetCount () {returnmdata.size (); } @Override PublicObject GetItem (intposition) { return NULL; } @Override Public LongGetitemid (intposition) { return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Convertview= Mlayoutinflater.inflate (R.layout.listitem,NULL);//instantiate view based on layout fileTextView TV =(TextView) Convertview.findviewbyid (R.id.textview); Tv.settext (Mdata.get (position). Get ("Index"). toString ()); ImageView img=(ImageView) Convertview.findviewbyid (R.id.imageview); Img.setbackgroundresource ((Integer) mdata.get (position). Get ("IMG")); returnConvertview; } }

6. Implement in the OnCreate function

        ListView = (ListView) Findviewbyid (R.id.listview);         // ----        Mdata = getData ();         New Myadapter (this);        Listview.setadapter (myadapter);

Iii. Summary

The entire implementation process is strictly in accordance with the MVC pattern, first of all to customize your own element layout, then get the data source, then rewrite the adapter, load the data source into the adapter, and display the adapter contents through the ListView.

The full version of the Java code is attached below:

 Packagecom.example.ListViewTest;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List; Public classMyActivityextendsActivity {/*** Called when the activity is first created. */@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); ListView=(ListView) Findviewbyid (R.id.listview); //----Mdata =GetData (); Myadapter=NewMyadapter ( This);    Listview.setadapter (Myadapter); }    PrivateList//M    PrivateListView ListView;//V    PrivateMyadapter Myadapter;//C    PrivateListGetData () {ArrayList<HashMap<String,Object>> arrayList =NewArraylist(); HashMap<String,Object> HashMap =NULL;  for(inti=0;i<10;i++) {HashMap=NewHashmap<string, object>(); Hashmap.put ("Index", integer.tostring (i+1)); Hashmap.put ("IMG", r.drawable.my); Arraylist.add (HASHMAP); //add to Data source        }        returnarrayList; }    //Create a custom adapter    Private classMyadapterextendsbaseadapter{PrivateLayoutinflater Mlayoutinflater;  PublicMyadapter (Context context) {Mlayoutinflater= Layoutinflater.from (context);//Dynamic Layout Mapping} @Override Public intGetCount () {returnmdata.size (); } @Override PublicObject GetItem (intposition) {            return NULL; } @Override Public LongGetitemid (intposition) {            return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Convertview= Mlayoutinflater.inflate (R.layout.listitem,NULL);//instantiate view based on layout fileTextView TV =(TextView) Convertview.findviewbyid (R.id.textview); Tv.settext (Mdata.get (position). Get ("Index"). toString ()); ImageView img=(ImageView) Convertview.findviewbyid (R.id.imageview); Img.setbackgroundresource ((Integer) mdata.get (position). Get ("IMG")); returnConvertview; }    }}

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.