2.4.10 可展开的列表组件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >



    <ExpandableListView

        android:id="@+id/expandableListView1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

    </ExpandableListView>



</LinearLayout>
package com.moon.hello2;



import java.util.ArrayList;

import java.util.Calendar;

import java.util.HashMap;

import java.util.List;

import java.util.Map;



import android.R.anim;

import android.R.integer;

import android.app.Activity;

import android.database.DataSetObserver;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.Gravity;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.AbsListView;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.ExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.ListView;

import android.widget.ProgressBar;

import android.widget.RatingBar;

import android.widget.SimpleAdapter;

import android.widget.RatingBar.OnRatingBarChangeListener;

import android.widget.SeekBar;

import android.widget.DatePicker.OnDateChangedListener;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.widget.TabHost;

import android.widget.TextView;

import android.widget.TimePicker;

import android.widget.TimePicker.OnTimeChangedListener;



public class MainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_main);

        ExpandableListView listView = (ExpandableListView)findViewById(R.id.expandableListView1);

        ExpandableListAdapter adapter = new ExpandableListAdapter() {

            private String[] armTypeStrings = new String[]{"神族","人族","虫族"};

            private String[][] armStrings = new String[][]{{"狂战士","龙骑士","黑暗圣堂","电兵"},{"小狗","刺蛇","飞龙","自爆飞机"},{"机枪兵","护士MM","幽灵"}};

            @Override

            public void unregisterDataSetObserver(DataSetObserver arg0) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            public void registerDataSetObserver(DataSetObserver arg0) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            public void onGroupExpanded(int arg0) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            public void onGroupCollapsed(int arg0) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            public boolean isEmpty() {

                // TODO Auto-generated method stub

                return false;

            }

            

            @Override

            public boolean isChildSelectable(int arg0, int arg1) {

                // TODO Auto-generated method stub

                return true;

            }

            

            @Override

            public boolean hasStableIds() {

                // TODO Auto-generated method stub

                return true;

            }

            

            @Override

            public View getGroupView(int groupPosition, boolean arg1, View arg2, ViewGroup arg3) {

                // TODO Auto-generated method stub

                LinearLayout linearLayout = new LinearLayout(MainActivity.this);

                linearLayout.setOrientation(LinearLayout.HORIZONTAL);

                TextView textView = this.getTextView();

                textView.setText(this.getGroup(groupPosition).toString());

                linearLayout.addView(textView);

                return linearLayout;

            }

            

            @Override

            public long getGroupId(int groupPosition) {

                // TODO Auto-generated method stub

                return groupPosition;

            }

            

            @Override

            public int getGroupCount() {

                // TODO Auto-generated method stub

                return this.armTypeStrings.length;

            }

            

            @Override

            public Object getGroup(int groupPosition) {

                // TODO Auto-generated method stub

                return this.armTypeStrings[groupPosition];

            }

            

            @Override

            public long getCombinedGroupId(long arg0) {

                // TODO Auto-generated method stub

                return 0;

            }

            

            @Override

            public long getCombinedChildId(long arg0, long arg1) {

                // TODO Auto-generated method stub

                return 0;

            }

            

            @Override

            public int getChildrenCount(int groupPosition) {

                // TODO Auto-generated method stub

                return this.armStrings[groupPosition].length;

            }

            

            @Override

            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,

                    ViewGroup parent) {

                TextView textView = this.getTextView();

                textView.setText(this.getChild(groupPosition, childPosition).toString());

                return textView;

            }

            

            @Override

            public long getChildId(int arg0, int arg1) {

                // TODO Auto-generated method stub

                return 0;

            }

            

            @Override

            public Object getChild(int groupPostion, int childPosition) {

                // TODO Auto-generated method stub

                return this.armStrings[groupPostion][childPosition];

            }

            

            @Override

            public boolean areAllItemsEnabled() {

                // TODO Auto-generated method stub

                return false;

            }

            

            private TextView getTextView(){

                TextView textView = new TextView(MainActivity.this);

                AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);

                textView.setLayoutParams(layoutParams);

                textView.setGravity(Gravity.CENTER_VERTICAL);

                textView.setPadding(36, 0, 0, 0);

                textView.setTextSize(20);

                return textView;

            }

        };

        listView.setAdapter(adapter);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }



}

getChildView 返回子列表中的单个view

getGroupView 返回单个group的view(不展开时候的那个)

childCount 返回某个group下边的子元素数目

groupCount 返回所有的group数目

你可能感兴趣的:(组件)