Simple android ExpandableListView Application

Source: Internet
Author: User

First of all, let's take a look at some cases. I still use the Sohu news client, because I have nothing to do with reading this stuff every day, because I don't have time to watch the news at work, so I can browse the news and pass the time when I go to work.

Let's take a look at this effect. It's not difficult to implement it. I 'd like to explain it briefly.
The first control we use is ExpandableListView.
Layout file:
[Java]
<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">
 
<! --
Android: groupIndicator = "@ null" cancel default image
Android: childIndicatorLeft
Android: dividerHeight must be set. Otherwise, the split line cannot be displayed. The default value is 0.
Android: childDivider = "@ drawable/child_bg". This directly introduces the color, or the picture will make the entire child's background color. I don't know why. If anyone knows, please say Give me.
-->
 
<ExpandableListView
Android: id = "@ + id/expandablelist"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: cacheColorHint = "@ null"
Android: childDivider = "@ drawable/child_bg"
Android: childIndicatorLeft = "0dp"
Android: divider = "@ color/Grey"
Android: dividerHeight = "1dp"
Android: groupIndicator = "@ null"
Android: scrollbarAlwaysDrawHorizontalTrack = "true">
</ExpandableListView>
 
</RelativeLayout>
MyexpandableListAdapter. java
[Java]
/***
* Data Source
*
* @ Author Administrator
*
*/
Class MyexpandableListAdapter extends BaseExpandableListAdapter {
Private Context context;
Private LayoutInflater inflater;
 
Public MyexpandableListAdapter (Context context ){
This. context = context;
Inflater = LayoutInflater. from (context );
}
 
// Returns the number of parent lists
@ Override
Public int getGroupCount (){
Return groupList. size ();
}
 
// Returns the number of sublists.
@ Override
Public int getChildrenCount (int groupPosition ){
Return childList. get (groupPosition). size ();
}
 
@ Override
Public Object getGroup (int groupPosition ){
 
Return groupList. get (groupPosition );
}
 
@ Override
Public Object getChild (int groupPosition, int childPosition ){
Return childList. get (groupPosition). get (childPosition );
}
 
@ Override
Public long getGroupId (int groupPosition ){
Return groupPosition;
}
 
@ Override
Public long getChildId (int groupPosition, int childPosition ){
Return childPosition;
}
 
@ Override
Public boolean hasStableIds (){
 
Return true;
}
 
@ Override
Public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent ){
GroupHolder groupHolder = null;
If (convertView = null ){
GroupHolder = new GroupHolder ();
ConvertView = inflater. inflate (R. layout. group, null );
GroupHolder. textView = (TextView) convertView
. FindViewById (R. id. group );
GroupHolder. imageView = (ImageView) convertView
. FindViewById (R. id. image );
GroupHolder. textView. setTextSize (15 );
ConvertView. setTag (groupHolder );
} Else {
GroupHolder = (GroupHolder) convertView. getTag ();
}
 
GroupHolder. textView. setText (getGroup (groupPosition). toString ());
If (isExpanded) // ture is Expanded or false is not isExpanded
GroupHolder. imageView. setImageResource (R. drawable. expanded );
Else
GroupHolder. imageView. setImageResource (R. drawable. collapse );
Return convertView;
}
 
@ Override
Public View getChildView (int groupPosition, int childPosition,
Boolean isLastChild, View convertView, ViewGroup parent ){
If (convertView = null ){
ConvertView = inflater. inflate (R. layout. item, null );
}
TextView textView = (TextView) convertView. findViewById (R. id. item );
TextView. setTextSize (13 );
TextView. setText (getChild (groupPosition, childPosition). toString ());
Return convertView;
}
 
@ Override
Public boolean isChildSelectable (int groupPosition, int childPosition ){
Return true;
}
}
 
@ Override
Public boolean onGroupClick (final ExpandableListView parent, final View v,
Int groupPosition, final long id ){
 
Return false;
}
The above implementation is relatively simple. I believe that friends who are familiar with listview will be very familiar with this, and there will be an extra child.
Selector_group.xml
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
 
<Item android: drawable = "@ color/Grey" android: state_pressed = "true"> </item>
<Item android: drawable = "@ color/Grey" android: state_selected = "true"> </item>
<Item android: drawable = "@ color/LightGray"> </item>
 
</Selector>
The same applies to selector_item.xml.

:

Although the effect is ugly, this is the case. You can customize the display group item or the child item at will.
You can download the source code and make some adjustments.

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.