Expandablelistview to achieve accordion effect

This example shares the specific code of expandablelistview to realize the accordion effect for your reference. The specific contents are as follows

1. Effect example drawing

2. Create method

(1) The first method is the same as ordinary controls such as listview. You can directly add expandablelistview control to the layout file.

(2) The second method is to create an activity that inherits from expandablelistactivity, and then obtain an expandablelistview object through getexpandablelistview() method.

The second method is only applicable when there is only one expandablelistview in a page. The inherited activity does not need to call the setcontentview () method. A system defined layout file has been associated with the expandablelistactivity.

3. Some properties and click events

Android: groupindicator, Android: childindicator: icons in front of group entries and sub entries. The default value is arrow. You can set custom image resources. If the icon is not displayed, it is set to @ null.

Android: divider, Android: childdivider: separator for groups and subitems.

There are two click events in expandablelistview, corresponding to group and sub item click events respectively:

Set the click event of the group: setongroupclicklistener (ongroupclicklistener listener)

Set click event of sub item: setonchildclicklistener (onchildclicklistener listener)

5. Adapter

According to different data sources, two adapters can be used: baseexpandablelistadapter and cursortreeadapter. Cursortreeader is used when the data source is cursor object, and baseexpandablelistadapter is used in other cases.

(1) Baseextendablelistadapter needs to override the following methods:

Getgroup (): get the data content of the group from the data source.

Getgroupcount(): get the total number of groups.

Getgroupid(): get the ID of the group.

Getgroupview(): get the view of the group.

Getchild(): get the content of the subitem from the data source.

Getchildcount(): get the total number of child entries in the specified group, not all of them.

Getchildid(): get the ID of the subentry.

Getchildview(): get the view of sub items

Hasstableid(): determines whether the entry corresponding to ID has been drawn, which is used to optimize the list.

Ischildselectable(): whether the sub item is allowed to be clicked. If false is returned, the sub item clicking event is invalid.

(2) Cursortreeadapter method to be overridden:

Cursortreeadapter(): the cursor object passed into the group by the constructor.

Getchildrencursor(): pass in the cursor object of the group and get the cursor object of the corresponding group's sub items.

Newgroupview(): creates a view of the group and returns a new view.

Bindgroupview(): bind the data content of the group view here. The first parameter is the return value of the newgroupview() method.

Newchildview(): creates a view of the subitem.

Bindchildview(): bind the data content of the child entry view.

6. Simple examples

Realize the example in the effect diagram.

Layout:

Activity:

Tool class for loading test data:

Adapter:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>