Simple Grouping in Android ListView (title with picture)

Source: Internet
Author: User

Idea: Add a simpleadapter,simpleadapter in the ListView to dynamically add headlines and headings under the headline, followed by ideas.

First step: Create a ListView layout file List.xml

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Horizontal"6Tools:context= ". Mainactivity ">7<ListView8Android:id= "@+id/list"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "Wrap_content" > One</ListView> A</LinearLayout>

Step two: Build the List_items_menu.xml layout file

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/relativelayout01"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:paddingbottom= "4SP"Android:paddingleft= "12SP"Android:paddingright= "12SP" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Vertical" > <TextView Android:id= "@+id/itemtitle"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:layout_gravity= "Left"Android:layout_marginbottom= "4DP"Android:layout_margintop= "7DP"Android:text= "title"/> </LinearLayout> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <ImageView Android:id= "@+id/itemimage"Android:layout_width= "40SP"Android:layout_height= "40sp"/> <TextView Android:id= "@+id/xiaoitemtitle"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Small title"android:textsize= "15sp"/> </LinearLayout></RelativeLayout>

Step three: Building activity classes

 PackageCom.example.test_menu;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportCom.example.test_menu. R;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ListView;Importandroid.widget.RelativeLayout;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.TextView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.Toast; Public  classTestextendsActivity {ArrayListListItem; HashMap<string, object>map;    Simpleadapter Listitemadapter;    ListView list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //The Settings screen has no title         This. Requestwindowfeature (Window.feature_no_title); //Remove title bar         This. GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLS        Creen);                        Setcontentview (r.layout.list); //bind the ListView inside the layoutList =(ListView) Findviewbyid (r.id.list); //generating dynamic arrays, adding dataListItem =NewArraylist(); String[] a={"Pipe network dynamic", "Construction daily", "notice announcement"}; String[] B={"Operation Management", "Operation Monitoring", "Path simulation optimization", "material Move Job", "Valve switch Management"}; String[] C={"Patrol Management", "Inspection Plan", "Patrol Record", "Patrol monitoring"}; String[] D={"Special Management", "Field station management", "Pipeline management", "Pipe Gallery Management", "Valve management"}; //icon in front of a small title        int[] Image_a={R.drawable.ic_launcher,r.drawable.lcmn,r.drawable.lct,r.drawable.ic_launcher};        Map (a,image_a);        Map (b,image_a);        Map (c,image_a);         Map (d,image_a); Listitemadapter=NewSimpleadapter ( This, ListItem,//Data SourceR.layout.list_items_menu,//XML implementation of ListItem//the children of the dynamic array corresponding to the Imageitem                NewString[] {"Itemtitle", "Itemimage", "Xiaoitemtitle" },                //a Imageview,textview ID in the imageitem XML file                New int[] {r.id.itemtitle,r.id.itemimage, r.id.xiaoitemtitle}); //Add and showList.setadapter (Listitemadapter); //Add Menu clickList.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,LongArg3) {                 for(inti = 0; I < listitem.size (); i++) {                                    if(Arg2 = =i) {Toast.maketext (Getapplicationcontext (), Listitem.get (ARG2). Get ("Xiaoitemtitle") + "" +arg2, Toast.length_short). Show ();            }                }            }        }); }     Public voidMap (string[] A,int[] Image) {             for(inti=0;i<a.length;i++) {Map=NewHashmap<string, object>();  for(intj=0;j<image.length;j++){                                if(i==0) {Map.put ("Itemtitle", a[0]); }Else if(i== (j+1) {map.put ("Itemimage", Image[j]);//ID of the image resourceMap.put ("Xiaoitemtitle", A[i]);        }} listitem.add (map); }            }      }

The effect is as follows:

Novice small ideas, please forgive us!

Simple Grouping in Android ListView (title with picture)

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.