首页 > 代码库 > 安卓学习-activity-ExpandableListActivity

安卓学习-activity-ExpandableListActivity

ExpandableListActivity

将所有应用程序按a-z分组,展开并点击启动程序

 

group.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" android:padding="5dp">    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="组"        android:textAppearance="?android:attr/textAppearanceLarge" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginLeft="16dp"        android:layout_toRightOf="@+id/textView1"        android:text="A"        android:textAppearance="?android:attr/textAppearanceLarge" />    <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginLeft="17dp"        android:layout_toRightOf="@+id/textView2"        android:text="共"        android:textAppearance="?android:attr/textAppearanceLarge" />    <TextView        android:id="@+id/textView4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_toRightOf="@+id/textView3"        android:text="0"        android:textAppearance="?android:attr/textAppearanceLarge" />    <TextView        android:id="@+id/textView5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_toRightOf="@+id/textView4"        android:text="项"        android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>
View Code

item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingTop="20dp"    android:paddingBottom="20dp"     >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:paddingRight="20dp"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_toRightOf="@+id/imageView1"        android:text="Large Text"        android:textAppearance="?android:attr/textAppearanceLarge" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:text="Small Text"        android:textAppearance="?android:attr/textAppearanceSmall" /></RelativeLayout>
View Code

MainActivity.java

public class MainActivity extends ExpandableListActivity {    private List<String> data=http://www.mamicode.com/new ArrayList<String>();    private List<List<Map<String,Object>>> data_c=new ArrayList<List<Map<String,Object>>>();    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置26个字母        for(int k=97;k<97+26;k++){            String zm=(char)k+"";            data.add(zm);                        //获取应用程序按26个字母分组            List<PackageInfo> pags = getPackageManager().getInstalledPackages(0);            List<Map<String, Object>> data1 = new ArrayList<Map<String, Object>>();            for (int i = 0; i < pags.size(); i++) {                PackageInfo pag = pags.get(i);                if(pag.packageName.substring(pag.packageName.lastIndexOf(".")+1).substring(0,1).equals(zm)){                        Map<String, Object> map = new HashMap<String, Object>();                        // 程序名字                        map.put("name",pag.applicationInfo.loadLabel(getPackageManager()).toString());                        // 图标                        map.put("drawable",pag.applicationInfo.loadIcon(getPackageManager()));                        map.put("packageName", pag.packageName);                        data1.add(map);                }            }            data_c.add(data1);        }        BaseExpandableListAdapter adapter=new BaseExpandableListAdapter(){            @Override            public int getGroupCount() {                // TODO 自动生成的方法存根                return data.size();            }            @Override            public int getChildrenCount(int groupPosition) {                // TODO 自动生成的方法存根                return data_c.get(groupPosition).size();            }            @Override            public Object getGroup(int groupPosition) {                // TODO 自动生成的方法存根                return null;            }            @Override            public Object getChild(int groupPosition, int childPosition) {                // TODO 自动生成的方法存根                return null;            }            @Override            public long getGroupId(int groupPosition) {                // TODO 自动生成的方法存根                return 0;            }            @Override            public long getChildId(int groupPosition, int childPosition) {                // TODO 自动生成的方法存根                return 0;            }            @Override            public boolean hasStableIds() {                // TODO 自动生成的方法存根                return false;            }            @Override            public View getGroupView(int groupPosition, boolean isExpanded,                    View convertView, ViewGroup parent) {                if(convertView==null){                    convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.group, null);                }                TextView tv1=(TextView)convertView.findViewById(R.id.textView2);                TextView tv3=(TextView)convertView.findViewById(R.id.textView4);                tv1.setText(data.get(groupPosition));                tv3.setText(data_c.get(groupPosition).size()+"");                        return convertView;            }            @Override            public View getChildView(int groupPosition, int childPosition,                    boolean isLastChild, View convertView, ViewGroup parent) {                if(convertView==null){                    convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);                }                ImageView iv1=(ImageView)convertView.findViewById(R.id.imageView1);                TextView tv2=(TextView)convertView.findViewById(R.id.textView1);                TextView tv4=(TextView)convertView.findViewById(R.id.textView2);                tv2.setText(data_c.get(groupPosition).get(childPosition).get("name")+"");                tv4.setText(data_c.get(groupPosition).get(childPosition).get("packageName")+"");                iv1.setImageDrawable((Drawable)data_c.get(groupPosition).get(childPosition).get("drawable"));                         return convertView;            }            @Override            public boolean isChildSelectable(int groupPosition,                    int childPosition) {                // TODO 自动生成的方法存根                return true;            }            };                setListAdapter(adapter);        //添加单击事件        getExpandableListView().setOnChildClickListener(new OnChildClickListener() {                        @Override            public boolean onChildClick(ExpandableListView parent, View v,                    int groupPosition, int childPosition, long id) {                                 Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(                            data_c.get(groupPosition).get(childPosition).get("packageName")+"");                 startActivity(intent);                return true;            }        });    }}
View Code

 

安卓学习-activity-ExpandableListActivity