android ExpandableListActivity简介

来源:互联网 发布:windows dpi 修改工具 编辑:程序博客网 时间:2024/06/05 20:10
主函数package com.example.expandlistactivity;import java.util.ArrayList;import java.util.List;import android.app.ExpandableListActivity;import android.os.Bundle;import android.widget.ExpandableListView;public class MainActivity extends ExpandableListActivity  { @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          List<String> gl = new ArrayList<String>();          List<List<String>> cl = new ArrayList<List<String>>();          gl.add("gr1");          gl.add("gr2");          gl.add("gr3");            List<String> chil1 = new ArrayList<String>();          chil1.add("c11");          chil1.add("c12");          chil1.add("c13");          chil1.add("c14");          chil1.add("c15");          chil1.add("c16");          chil1.add("c17");          chil1.add("c18");          List<String> chil2 = new ArrayList<String>();          chil2.add("c21");          chil2.add("c22");          chil2.add("c23");          chil2.add("c24");          chil2.add("c25");          chil2.add("c26");          chil2.add("c27");          chil2.add("c28");          List<String> chil3 = new ArrayList<String>();          chil3.add("c31");          chil3.add("c32");          chil3.add("c33");          chil3.add("c34");          chil3.add("c35");          chil3.add("c36");          chil3.add("c37");          chil3.add("c38");            cl.add(chil1);          cl.add(chil2);          cl.add(chil3);            adapter e = new adapter(this, gl, cl);            ExpandableListView expandableListView = getExpandableListView();            // 设置适配器          expandableListView.setAdapter(e);          // 设置组图标 可以使用selector          expandableListView.setGroupIndicator(getResources().getDrawable(                  R.drawable.away));          // 设置组图标位置          expandableListView.setIndicatorBounds(0, 180);          // 设置子项图标          expandableListView.setChildIndicator(getResources().getDrawable(                  R.drawable.busy));          // 设置子项图标位置          expandableListView.setChildIndicatorBounds(0, 180);      }  }


显示expandableListView的adapter

package com.example.expandlistactivity;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class adapter extends BaseExpandableListAdapter {        List<String> GroupList = new ArrayList<String>();      List<List<String>> ChildrenList = new ArrayList<List<String>>();      Context context = null;        public adapter(Context c, List<String> gl, List<List<String>> cl) {          context = c;          GroupList.addAll(gl);          ChildrenList.addAll(cl);     }        // 返回组的总数      @Override      public int getGroupCount() {          return GroupList.size();      }        // 返回子项总数      @Override      public int getChildrenCount(int groupPosition) {          return ChildrenList.get(groupPosition).size();      }        // 返回组对象      @Override      public Object getGroup(int groupPosition) {          return GroupList.get(groupPosition);      }        // 返回子项对象      @Override      public Object getChild(int groupPosition, int childPosition) {          return ChildrenList.get(groupPosition).get(childPosition);      }        // 返回组ID      @Override      public long getGroupId(int groupPosition) {          return groupPosition;      }        // 返回子项ID      @Override      public long getChildId(int groupPosition, int childPosition) {          return childPosition;      }        // 是否具有固定ID      @Override      public boolean hasStableIds() {          return false;      }        // 返回组VIEW      @Override      public View getGroupView(int groupPosition, boolean isExpanded,              View convertView, ViewGroup parent) {          TextView tv = new TextView(context);          tv.setText(GroupList.get(groupPosition));          tv.setHeight(40);          return tv;      }        // 返回子项VIEW      @Override      public View getChildView(int groupPosition, int childPosition,              boolean isLastChild, View convertView, ViewGroup parent) {          TextView tv = new TextView(context);  //        for(int i=0;i<childPosition + 1;i++){//这两种方式都可以在页面显示child//            tv.setText(ChildrenList.get(groupPosition).get(i));  //            tv.setHeight(40);  //        }         tv.setText(ChildrenList.get(groupPosition).get(childPosition));          tv.setHeight(40);          return tv;      }        // 子项是否可以被选中      @Override      public boolean isChildSelectable(int groupPosition, int childPosition) {          return true;      }  }  

xml文件

<?xml version="1.0" encoding="utf-8"?><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/android:list"        android:layout_width="fill_parent"        android:layout_height="wrap_content" >    </ExpandableListView></LinearLayout>


0 0
原创粉丝点击