Android ExpandableListView, which one is displayed, and other hidden implementations

Source: Internet
Author: User

:
Directly run the Code [java] public class MyActivity extends Activity implements HandleClick {private ExpandableListView listView; private int lastClick =-1; // position public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); listView = (ExpandableListView) findViewById (R. id. listView); // remove the previous arrow icon listView. setGroupIndicator (null); MyExpandAdapt Er adapter = new MyExpandAdapter (this); adapter. setHnadleClick (this); listView. setAdapter (adapter); listView. setOnGroupClickListener (new OnGroupClickListener () {public boolean onGroupClick (ExpandableListView parent, View v, int groupPosition, long id) {if (lastClick =-1) {listView. expandGroup (groupPosition);} if (lastClick! =-1 & lastClick! = GroupPosition) {listView. collapseGroup (lastClick); listView. expandGroup (groupPosition);} else if (lastClick = groupPosition) {if (listView. isGroupExpanded (groupPosition) listView. collapseGroup (groupPosition); else if (! ListView. isGroupExpanded (groupPosition) listView. expandGroup (groupPosition) ;}lastclick = groupPosition; return true ;}});} public void handleClick (int type) {Toast. makeText (MyActivity. this ,"... clicked... ", Toast. LENGTH_SHORT ). show () ;}} adapter: [java] public class MyExpandAdapter extends BaseExpandableListAdapter {private Context context; private String [] roomName = new String [] {"Room 1 ", "Room 2", "Room 3", "Room 4", "Room 5"}; private String [] peoNum = new String [] {"1323 people ", "6654", "86554", "6565", "7967"}; private String [] startTime = new String [] {"Start now ", "1 minute", "2 minutes", "3 minutes", "4 minutes"}; private HandleClick handleClick; public interface HandleClick {public void handleClick (int type );} public void setHnadleClick (HandleClick hc) {this. handleClick = hc;} public MyExpandAdapter (Context context) {this. context = context;} public boolean isChildSelectable (int groupPosition, int childPosition) {// TODO Auto-generated method stub return false;} @ Override public boolean hasStableIds () {// TODO Auto-generated method stub return false;} @ Override public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {convertView = (RelativeLayout) RelativeLayout. inflate (context, R. layout. group, null); TextView TV _roomName = (TextView) convertView. findViewById (R. id. TV _roomName); TextView TV _time = (TextView) convertView. findViewById (R. id. TV _time); TextView TV _peoNum = (TextView) convertView. findViewById (R. id. TV _peoNum); values (roomName [groupPosition]); TV _time.setText (peoNum [groupPosition]); values (startTime [groupPosition]); return convertView;} @ Override public long getGroupId (int groupPosition) {// TODO Auto-generated method stub return 0;} @ Override public int getGroupCount () {// TODO Auto-generated method stub return roomName. length ;}@ Override public Object getGroup (int groupPosition) {// TODO Auto-generated method stub return null;} @ Override public int getChildrenCount (int groupPosition) {// TODO Auto-generated method stub return 1;} @ Override public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {convertView = (LinearLayout) linearLayout. inflate (context, R. layout. child, null); TextView child_startTime = (TextView) convertView. findViewById (R. id. child_startTime); TextView child_peoNum = (TextView) convertView. findViewById (R. id. child_peoNum); Button child_button = (Button) convertView. findViewById (R. id. child_button); child_startTime.setText (startTime [groupPosition]); child_peoNum.setText (peoNum [groupPosition]); listener (new OnClickListener () {public void onClick (View v) {handleClick. handleClick (1) ;}}); return convertView;} public long getChildId (int groupPosition, int childPosition) {// TODO Auto-generated method stub return 0 ;} @ Override public Object getChild (int groupPosition, int childPosition) {// TODO Auto-generated method stub return null ;}}

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.