[Android Notes] use of ExpandableListView, Android listview

Source: Internet
Author: User

[Android Notes] use of ExpandableListView, Android listview

Effect:

Expanded ListView



In fact, there is no difference with the normal ListView, but the ListView is changed to ExpandableListView, And the adapter is changed from BaseAdapter to BaseExpandableListAdapter.
Steps:
1. Write the layout file.
There are three types: Master Layout, group layout, and group item layout: Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.expandablelistviewdemo.MainActivity" >    <ExpandableListView        android:id="@+id/elv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:divider="#000"        android:dividerHeight="2.5dp"        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft" >    </ExpandableListView></RelativeLayout>

List_group.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/listTitle"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:paddingBottom="10dp"        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"        android:paddingTop="10dp"        android:textColor="#A4C739" /></LinearLayout>
List_item.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/expandedListItem"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:paddingBottom="10dp"        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"        android:paddingTop="10dp" /></LinearLayout>
2. Compile the adapter code:

Package com. example. expandablelistviewdemo; import java. util. hashMap; import java. util. list; import android. content. context; import android. view. view; import android. view. viewGroup; import android. widget. baseExpandableListAdapter; import android. widget. textView; public class extends BaseExpandableListAdapter {private Context mContext; private List <String> expandableListTitle; private HashMap <String, List <String> external; public MyExpandableListAdapter (List <String> expandableListTitle, hashMap <String, List <String> expandableListDetail, Context mContext) {this. expandableListDetail = expandableListDetail; this. expandableListTitle = expandableListTitle; this. mContext = mContext;} @ Overridepublic int getGroupCount () // number of groups {return this. expandableListTitle. size () ;}@ Overridepublic int getChildrenCount (int groupPosition) // The number of items in the Group {return this. expandableListDetail. get (expandableListTitle. get (groupPosition )). size () ;}@ Overridepublic Object getGroup (int groupPosition) // obtain group data {return this. expandableListTitle. get (groupPosition) ;}@ Overridepublic Object getChild (int groupPosition, int childPosition) // obtain the data of the items in the nth Group {return this. expandableListDetail. get (this. expandableListTitle. get (groupPosition )). get (childPosition) ;}@ Overridepublic long getGroupId (int groupPosition) {return groupPosition ;}@ Overridepublic long getChildId (int groupPosition, int childPosition) {return childPosition ;} @ Overridepublic boolean hasStableIds () {return false ;}@ Overridepublic View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {String data = this. expandableListTitle. get (groupPosition); if (convertView = null) {convertView = View. inflate (mContext, R. layout. list_group, null);} TextView TV = (TextView) convertView. findViewById (R. id. listTitle); TV. setText (data); return convertView;} @ Overridepublic View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {// String data = this. expandableListDetail. get (this. expandableListTitle. get (groupPosition )). get (childPosition); String data = (String) this. getChild (groupPosition, childPosition); if (convertView = null) {convertView = View. inflate (mContext, R. layout. list_item, null);} TextView TV = (TextView) convertView. findViewById (R. id. expandedListItem); TV. setText (data); return convertView;} @ Overridepublic boolean isChildSelectable (int groupPosition, int childPosition) {return true ;}}

As you can see, the adapter has two getView methods to construct the view of group and child respectively.
3. Construct a data source
package com.example.expandablelistviewdemo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class ExpandableListDataPump{public static HashMap<String, List<String>> getData(){HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();List<String> technology = new ArrayList<String>();technology.add("Beats sued for noise-cancelling tech");technology.add("Wikipedia blocks US Congress edits");technology.add("Google quizzed over deleted links");technology.add("Nasa seeks aid with Earth-Mars links");technology.add("The Good, the Bad and the Ugly");List<String> entertainment = new ArrayList<String>();entertainment.add("Goldfinch novel set for big screen");entertainment.add("Anderson stellar in Streetcar");entertainment.add("Ronstadt receives White House honour");entertainment.add("Toronto to open with The Judge");entertainment.add("British dancer return from Russia");List<String> science = new ArrayList<String>();science.add("Eggshell may act like sunblock");science.add("Brain hub predicts negative events");science.add("California hit by raging wildfires");science.add("Rosetta's comet seen in close-up");science.add("Secret of sandstone shapes revealed");expandableListDetail.put("TECHNOLOGY NEWS", technology);expandableListDetail.put("ENTERTAINMENT NEWS", entertainment);expandableListDetail.put("SCIENCE & ENVIRONMENT NEWS", science);return expandableListDetail;}}

4. Compile the code on the main interface
Package com. example. expandablelistviewdemo; import java. util. arrayList; import java. util. hashMap; import java. util. list; import android. annotation. suppressLint; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. expandableListView; import android. widget. expandableListView. onChildClickListener; import android. widget. expandableListView. onGroupCollapseListener; import android. widget. expandableListView. onGroupExpandListener; import android. widget. toast; public class MainActivity extends Activity {private ExpandableListView elv = null; private MyExpandableListAdapter adapter = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); elv = (ExpandableListView) findViewById (R. id. elv); final HashMap <String, List <String> itemData = ExpandableListDataPump. getData (); final List <String> title = new ArrayList <String> (itemData. keySet (); adapter = new MyExpandableListAdapter (title, itemData, this); elv. setAdapter (adapter); // remove the arrow // elv. setGroupIndicator (null); // shrink elv. setOnGroupCollapseListener (new OnGroupCollapseListener () {@ Overridepublic void onGroupCollapse (int groupPosition) {Toast. makeText (MainActivity. this, title. get (groupPosition) + "group collapse ..... ", 0 ). show () ;}}); // stretch elv. setOnGroupExpandListener (new OnGroupExpandListener () {@ Overridepublic void onGroupExpand (int groupPosition) {Toast. makeText (MainActivity. this, title. get (groupPosition) + "group expand... ", 0 ). show () ;}}); // click elv for a subentry. setOnChildClickListener (new OnChildClickListener () {@ Overridepublic boolean onChildClick (ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast. makeText (MainActivity. this, itemData. get (title. get (groupPosition )). get (childPosition) + "click", 0 ). show (); return false ;}});}}

OK. Note that ExpandableListView has multiple listeners, such as group shrinking and stretching, and subentry clicking. In addition, an arrow is displayed by default. You can use the setGroupIndicator method to set the id you need.
Source Code address: https://github.com/Rowandjj/ExpandableListViewDemo



Related Article

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.