Android list shrink and expand faux QQ Friends List (very detailed, attached source)

Source: Internet
Author: User

Friends QQ list, you can expand, can be put up, in Android, used to be more than the ListView, although the list can be implemented, but in some cases, we still want to use the list can be grouped and to achieve contraction, That will use the Android Expandablelistview, today studied the use of this, but also refer to a lot of data to write a small demo, to achieve the basic functions, the following direct and source code ~!

Expandablelistview is a vertical scrolling view of level two list items, and unlike a ListView, it can have two layers: each layer can be expanded independently and display its children.

The effect of this article:

Effect one source download (resources are swallowed, come again tomorrow)

Effect two source download (resources are swallowed, come again tomorrow)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/DB/wKiom1TshZSxmF5uAABK3felP_0041.gif "title=" A60.gif "alt=" Wkiom1tshzsxmf5uaabk3felp_0041.gif "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/DB/wKiom1Tsh1Xz0R58AALzxxcq1RM817.gif "title=" A61.gif "alt=" Wkiom1tsh1xz0r58aalzxxcq1rm817.gif "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/DB/wKiom1TshZXh68ytAABMSOsLJsc421.gif "title=" A63.gif "alt=" Wkiom1tshzxh68ytaabmsosljsc421.gif "/>

Directory:

First, the realization principle

Second, layout and code

Three, custom icons

Four, the icon placed to the right

First, the realization principle

1, you must first define a Expandablelistview in the layout file

2, next create the layout file corresponding to the first level entry group

3. Create a two-level entry corresponding to the layout file child

4, the activity that loads the Expandablelistview component must inherit from expandablelistactivity

Second, layout and code

1, first in the main layout activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <expandablelistview android:id= "@i D/android:list "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "/></LinearLayou T>

2, next define the layout-level list in the Drawable folder Groups.xml

<linearlayout    xmlns:android= "Http://schemas.android.com/apk/res/android"      android:orientation= "vertical"     android:layout_width= "fill_parent"      android:layout_height= "Fill_parent" >    <TextView         android:id= "@+id/textgroup"          android:layout_width= "Fill_parent"         android:layout_height= "Fill_parent"         android:paddingleft= "40px"          android:paddingtop= "6px"         android: Paddingbottom= "6px"         android:textsize= "25SP"          android:text= "No data"     /></LinearLayout>

3. Then define the layout level two list in the Drawable folder Childs.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" > <textview android:id= "@+id/tex        Tchild "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:paddingleft=" 60px " android:paddingtop= "10px" android:paddingbottom= "10px" android:textsize= "20sp" android:text= "No Dat A "/></linearlayout>

4, then initialize and use the

package com.example.expandablelistview;import java.util.arraylist;import java.util.hashmap; import java.util.list;import java.util.map;import android.os.bundle;import  android.app.expandablelistactivity;import android.util.displaymetrics;import android.view.view; Import android.widget.expandablelistview;import android.widget.simpleexpandablelistadapter;import  android.widget.toast;public class mainactivity extends expandablelistactivity { /** *  Creating a Level entry container  */List<Map<String, String>> gruops = new  Arraylist<map<string, string>> ();/** *  Store content,  for display in list  */List<List< Map<string, string>>> childs = new arraylist<list<map<string,  String>>> @Overridepublic  void oncreate (bundle savedinstancestate)  { Super.oncreate (savedinstancestate); SetcontentvieW (r.layout.activity_main); Setlistdata ();} /** *  set List Contents  */public void setlistdata ()  {//  Create two level entry headings map<string,  String> title_1 = new hashmap<string, string> (); Map<string, string> title_2 = new hashmap<string, string> (); Map<string, string> title_3 = new hashmap<string, string> (); title_1. Put ("group",  "Lin Bingwen"), Title_2.put ("group",  "Wenbinglin"); Gruops.add (title_1); Gruops.add (title_2);//  Create level two entry content//  content one map<string, string> title_1_content_1 = new hashmap< String, string> (); Map<string, string> title_1_content_2 = new hashmap<string, string> ( ); Map<string, string> title_1_content_3 = new hashmap<string, string> ( ), Title_1_content_1.put ("Child",  "worker"), Title_1_content_2.put ("Child",  "student"); Title_1_coNtent_3.put ("Child",  "Farmer"); List<map<string, string>> childs_1 = new arraylist<map<string,  String>> Childs_1.add (title_1_content_1); Childs_1.add (title_1_content_2); Childs_1.add (title_1_ Content_3);//  content two map<string, string> title_2_content_1 = new hashmap< String, string> (); Map<string, string> title_2_content_2 = new hashmap<string, string> ( ); Map<string, string> title_2_content_3 = new hashmap<string, string> ( ), Title_2_content_1.put ("Child",  "orangutan"), Title_2_content_2.put ("Child",  "Tiger"); Title_2_content_3.put (" Child ", " lion "); List<map<string, string>> childs_2 = new arraylist<map<string,  String>> Childs_2.add (title_2_content_1); Childs_2.add (title_2_content_2); Childs_2.add (title_2_ Content_3); Childs.add (childs_1); Childs.add (childs_2);/** *  Create expandablelist Adapter containers   Parameters:  1. Context  2. First-level collection  3. First-level style files  4.  first-level entry key values  *  5. First level Display control name  6.  level Two collection  7.  level two style  8. Level two entry key value  9. Two level Display control name  *  */ Simpleexpandablelistadapter sela = new simpleexpandablelistadapter (This, gruops,  R.drawable.groups, new String[] {  "Group"  },new int[] {  r.id.textgroup }, childs, r.drawable.childs,new string[] {  "Child"  },  new int[] { r.id.textchild });//  join list setlistadapter (SELA);} /** *  List Contents Press  */@Overridepublic  boolean onchildclick (expandablelistview parent,  view v,int groupposition, int childposition, long id)  {Toast.makeText (Mainactivity.this, "You have selected" + gruops.get (groupposition). toString () +  "sub-number" + childs.get ( groupposition). Get (Childposition). toString (),  toast.length_short). show (); Return super.onchildclick (parent, v, groupposition, childposition, id);} /** *  Level Two title press  */@Overridepublic  boolean setselectedchild (int groupposition,  Int childposition,boolean shouldexpandgroup)  {return super.setselectedchild (GroupPosition,  childposition,shouldexpandgroup);} /** *  level title Press  */@Overridepublic  void setselectedgroup (int groupposition)  { Super.setselectedgroup (groupposition);}}

5. Effect

This is the effect on my phone, click Workers. Students and so on the level two list, my phone will have a prompt box appears, but I do not know why the record is not.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/DB/wKiom1TshZSxmF5uAABK3felP_0041.gif "title=" A60.gif "alt=" Wkiom1tshzsxmf5uaabk3felp_0041.gif "/>

Third, custom list icon

The above icon is generated by the system itself, below we want to change to their own

1. Change the custom icon

Create a new Expandablelistview_change.xml under the Drawable folder

<?xml Version = "1.0" encoding = "Utf-8"? ><selector xmlns:android = "Http://schemas.android.com/apk/res/andro ID "> <item android:state_expanded =" true "android:drawable =" @drawable/w2 "/> <item an droid:drawable = "@drawable/w1"/> </selector >

2, modify the above layout Activity.main.xml

<linearlayout xmlns:android=" Http://schemas.android.com/apk/res/android "     android:layout_width= "Fill_parent"     android:layout_height= "Fill_parent"      android:orientation= "Vertical"  >    <ExpandableListView         android:id= "@id/android:list"          android:layout_width= "Fill_parent"         android:layout_ height= "Fill_parent"         android:background= "#f5f5f5"          android:cachecolorhint= "#f5f5f5"          android:groupindicator= "@drawable/expandablelistview_change" &NBSP;/></LINEARLAYOUT> 

In fact, add a sentence android:groupindicator= "@drawable/expandablelistview_change"

Let's take a look at the results:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/DB/wKiom1Tsh1Xz0R58AALzxxcq1RM817.gif "title=" A61.gif "alt=" Wkiom1tsh1xz0r58aalzxxcq1rm817.gif "/>

Four, the icon placed to the right

In the above Mainactivity.java function Setlistdata () plus:

Get the size of the screen displaymetrics DM = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);// The icon is set to the right Getexpandablelistview (). Setindicatorbounds (dm.widthpixels-60, dm.widthpixels); Set the location of the indicator icon

Effect:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/DB/wKiom1TshZXh68ytAABMSOsLJsc421.gif "title=" A63.gif "alt=" Wkiom1tshzxh68ytaabmsosljsc421.gif "/>




This article is from the "Lin Bingwen blog Space" blog, be sure to keep this source http://linbingwen.blog.51cto.com/9912186/1615114

Android list shrink and expand faux QQ Friends List (very detailed, attached source)

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.