zoukankan      html  css  js  c++  java
  • 安卓学习-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=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
  • 相关阅读:
    从滴滴flinkCEP说起
    从零讲Java,给你一条清晰地学习道路!该学什么就学什么!
    从coalesce算子发散开的
    从架构理解价值-我的软件世界观
    为什么说Redis是单线程的?
    全排列_获取第几个排列_获取是第几个排列__康托展开,逆康托展开
    求排列是全排列的第几个,求第几个是全排列___康托展开,逆康托展开
    搜索入门_简单搜索bfs dfs大杂烩
    无根树同构_hash
    乘法逆元_三种方法
  • 原文地址:https://www.cnblogs.com/weijj/p/4137917.html
Copyright © 2011-2022 走看看