Use of android ExpandableListView

Source: Internet
Author: User

ExpandableListView, as its name implies, is a scalable ListView. It can be used to implement menu settings and other functions. The project was used some time ago. Now I want to share with you the implementation method of ExpandableListView.

First:

After expansion:

The layout file is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ExpandableListView android:id="@+id/newsList"android:layout_width="fill_parent"android:layout_height="fill_parent"android:cacheColorHint="@color/transparent"android:drawSelectorOnTop="false"android:groupIndicator="@null"></ExpandableListView></LinearLayout>

The layout of the parent topic of ExpandableListView is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="32dip"android:background="@drawable/bg_group"android:padding="5dip"><TextView android:id="@+id/group_text"android:textSize="22dip"android:textColor="@color/white"android:layout_width="wrap_content"android:layout_height="wrap_content"/><ImageView android:id="@+id/group_image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="10dip"android:paddingTop="3dip"/></LinearLayout>

Subtopic layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/white"android:padding="5dip"><TextView android:id="@+id/child"android:layout_marginLeft="10dip"android:textSize="22dip"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout>

The main code is as follows:

Package com. cloay. news; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. expandableListView; import android. widget. expandableListView. onChildClickListener; import com. cloay. news. bean. newsListAdapter; import com. cloay. news. service. mainService;/*** implement the topic list interface (implemented using ExpendListView) * @ author Cloay * 2012-3-3*09:38:12 */public class HomeActivity extends NewsReaderActivity {private ExpandableListView newsList; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. home); MainService. allActivity. add (this); newsList = (ExpandableListView) findViewById (R. id. newsList); NewsListAdapter adapter = new NewsListAdapter (HomeActivity. this); newsList. setAdapter (adapter); newsList. setOnChildClickListener (new OnChildClickListener () {// set listening for item. Note that isChildSelectable () return true must be in the adapter. For details, see the following code @ Overridepublic boolean onChildClick (ExpandableListView parent, View, int childPosition, long id) {// System. out. println ("groupPosition -->:" + groupPosition); // System. out. println ("childPosition -->:" + childPosition); Intent intent = new Intent (HomeActivity. this, ContentActivity. class); intent. putExtra ("groupPosition", groupPosition); intent. putExtra ("childPosition", childPosition); HomeActivity. this. startActivity (intent); return true ;}});}}

The Adapter that provides data for ExpandableListView:

Package com. cloay. news. bean; import java. util. arrayList; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseExpandableListAdapter; import android. widget. imageView; import android. widget. textView; import com. cloay. news. r; import com. cloay. news. constants. constants;/*** add ExpandableListView Data * @ author Cloay * 2012-3-4*08:10:29 */public class NewsListAdapter extends BaseExpandableListAdapter {@ SuppressWarnings ("rawtypes") private List <List> childList = new ArrayList <List> (); private LayoutInflater inflater; public NewsListAdapter (Context context) {super (); // initialize the data initData (context );} /*** provide data initialization for group and child * @ param context */private void initData (Context context) {// set the parent topic title Constants. Group. add (context. getResources (). getString (R. string. sina_news); Constants. group. add (context. getResources (). getString (R. string. sina_s); Constants. group. add (context. getResources (). getString (R. string. sina_t); Constants. group. add (context. getResources (). getString (R. string. sina_j); Constants. group. add (context. getResources (). getString (R. string. sina_e); Constants. group. add (context. getResources (). getStrin G (R. string. sina_f); // the title of each subtopic, Constants. child1.add (context. getResources (). getString (R. string. sina_china); Constants. child1.add (context. getResources (). getString (R. string. sina_world); Constants. child2.add (context. getResources (). getString (R. string. sina_s_all); Constants. child2.add (context. getResources (). getString (R. string. sina_s_football); Constants. child2.add (context. getResources (). getString (R. stri Ng. sina_s_nba); Constants. child3.add (context. getResources (). getString (R. string. sina_t); Constants. child3.add (context. getResources (). getString (R. string. sina_t_internet); Constants. child3.add (context. getResources (). getString (R. string. sina_t_mobile); Constants. child4.add (context. getResources (). getString (R. string. sina_j_all); Constants. child4.add (context. getResources (). getString (R. string. sina_j_china) ); Constants. child4.add (context. getResources (). getString (R. string. sina_j_world); Constants. child5.add (context. getResources (). getString (R. string. sina_e_all); Constants. child6.add (context. getResources (). getString (R. string. sina_f_w); Constants. child6.add (context. getResources (). getString (R. string. sina_f_m); childList. add (Constants. child1); childList. add (Constants. child2); childList. add (Constants. child3 ); ChildList. add (Constants. child4); childList. add (Constants. child5); childList. add (Constants. child6); inflater = LayoutInflater. from (context);} public NewsListAdapter (String [] group) {}@ Overridepublic int getGroupCount () {return Constants. group. size (); // parent column size} @ Overridepublic int getChildrenCount (int groupPosition) {return childList. get (groupPosition ). size (); // Number of subtopics} @ Overridepublic Object getGroup (int gr OupPosition) {return null ;}@ Overridepublic Object getChild (int groupPosition, int childPosition) {return null ;}@ Overridepublic long getGroupId (int groupPosition) {return 0 ;} @ Overridepublic long getChildId (int groupPosition, int childPosition) {return 0 ;}@ Overridepublic boolean hasStableIds () {return false ;}@ Overridepublic View getGroupView (int groupPosition, boolean isExpanded, view convertView, Vi EwGroup parent) {if (convertView = null) {convertView = inflater. inflate (R. layout. group, null);} ImageView image = (ImageView) convertView. findViewById (R. id. group_image); // you can specify the image icon for expansion. setImageResource (R. drawable. mm_submenu_down_normal); if (! IsExpanded) {image. setImageResource (R. drawable. mm_submenu_normal);} TextView textView = (TextView) convertView. findViewById (R. id. group_text); textView. setText (Constants. group. get (groupPosition); return convertView;} @ Overridepublic View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {if (convertView = null) {convertView = inflater. inflate (R. layout. child, null);} TextView textView = (TextView) convertView. findViewById (R. id. child); textView. setText (childList. get (groupPosition )). get (childPosition ). toString (); return convertView;} @ Overridepublic boolean isChildSelectable (int groupPosition, int childPosition) {return true; // return true to click child }}

Through the above code, we can achieve what we want. Please leave a message if you have any questions. Let's exchange and learn!

Note: Please indicate the source for reprinting!

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.