• 热门专题

IOS编程详解ExpandableListView组件的使用

作者:美好家园博客  发布日期:2012-09-18 09:32:31
Tag标签:IOS编程  ExpandableListView  
  • 直接上图:

     


     

    代码:

    定义是适配器

     

    package com.wsl.expandablelistview;  
     
    import android.content.Context;  
    import android.view.Gravity;  
    import android.view.View;  
    import android.view.ViewGroup;  
    import android.widget.AbsListView;  
    import android.widget.BaseExpandableListAdapter;  
    import android.widget.TextView;  
     
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {  
     
        private String[] groups={"我的好友","家人","同事","高中同学","初中同学"};  
        private String[][] children={{"王明","李乐","刘冲","卢广名"},{"王明","李乐","刘冲","卢广名"},{"王明","李乐","刘冲","卢广名"},{"王明","李乐","刘冲","卢广名"}};  
          
        private Context context=null;  
          
        public MyExpandableListAdapter(Context context){  
            this.context=context;  
        }  
          
        public Object getChild(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
            return this.children[groupPosition][childPosition];  
        }  
     
        public long getChildId(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
              
            return childPosition;  
        }  
          
          
        public TextView buildTextView(){  
              
            AbsListView.LayoutParams param=new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 35);  
            TextView textview=new TextView(this.context);  
            textview.setLayoutParams(param);  
            textview.setTextSize(15.0f);  
            textview.setGravity(Gravity.LEFT);  
            textview.setPadding(40, 8, 3, 3);  
              
            return textview;  
              
        }  
          
          
          
     
        public View getChildView(int groupPosition, int childPosition,  
                boolean isLastChild, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
            TextView textView=buildTextView();  
            textView.setText(getChild(groupPosition, childPosition).toString());  
            return textView;  
        }  
     
        public int getChildrenCount(int groupPosition) {  
            // TODO Auto-generated method stub  
            return this.children[groupPosition].length;  
        }  
     
        public Object getGroup(int groupPosition) {  
            // TODO Auto-generated method stub  
            return this.groups[groupPosition];  
        }  
     
        public int getGroupCount() {  
            // TODO Auto-generated method stub  
            return groups.length;  
        }  
     
        public long getGroupId(int groupPosition) {  
            // TODO Auto-generated method stub  
            return groupPosition;  
        }  
     
        public View getGroupView(int groupPosition, boolean isExpanded,  
                View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
            TextView textview=buildTextView();  
            textview.setText(this.getGroup(groupPosition).toString());  
            return textview;  
              
        }  
     
        public boolean hasStableIds() {  
            // TODO Auto-generated method stub  
            return true;  
        }  
     
        public boolean isChildSelectable(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
            return true;  
        }  
     
    }  
    
    

    定义布局管理器

     

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
     
        <ExpandableListView 
            android:id="@+id/elistview" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            /> 
     
    </LinearLayout> 
    
    
    

    定义Activity

     

    package com.wsl.expandablelistview;  
     
    import android.os.Bundle;  
    import android.app.Activity;  
    import android.view.ContextMenu;  
    import android.view.Menu;  
    import android.view.View;  
    import android.view.ContextMenu.ContextMenuInfo;  
    import android.widget.ExpandableListAdapter;  
    import android.widget.ExpandableListView;  
     
    import android.widget.ExpandableListView.OnChildClickListener;  
    import android.widget.ExpandableListView.OnGroupClickListener;  
    import android.widget.ExpandableListView.OnGroupCollapseListener;  
    import android.widget.ExpandableListView.OnGroupExpandListener;  
    import android.widget.Toast;  
     
    public class MainActivity extends Activity {  
     
          
        private ExpandableListView elistview=null;  
        private ExpandableListAdapter adapter=null;  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
              
            this.elistview=(ExpandableListView) super.findViewById(R.id.elistview);  
            this.adapter=new MyExpandableListAdapter(this);  
              
            this.elistview.setAdapter(adapter);  
            registerForContextMenu(this.elistview);  
              
            this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());  
            this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());  
            this.elistview.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());  
            this.elistview.setOnGroupExpandListener(new OnGroupExpandListenerImpl());  
        }  
          
          
        private class OnChildClickListenerImpl implements OnChildClickListener{  
     
            public boolean onChildClick(ExpandableListView parent, View v,  
                    int groupPosition, int childPosition, long id) {  
                  
                Toast.makeText(MainActivity.this, "分组被选中,groupposition"+childPosition+",childPosition="+childPosition, Toast.LENGTH_LONG).show();  
                return false;  
            }  
              
        }  
          
        private class OnGroupClickListenerImpl implements OnGroupClickListener{  
     
            public boolean onGroupClick(ExpandableListView parent, View v,  
                    int groupPosition, long id) {  
                // TODO Auto-generated method stub  
                Toast.makeText(MainActivity.this, "分组被选中,groupposition"+groupPosition, Toast.LENGTH_LONG).show();  
                return false;  
            }  
              
        }  
          
          
        public class OnGroupCollapseListenerImpl implements OnGroupCollapseListener{  
     
            public void onGroupCollapse(int groupPosition) {  
                // TODO Auto-generated method stub  
                Toast.makeText(MainActivity.this, "关闭分组,groupposition"+groupPosition, Toast.LENGTH_LONG).show();  
            }  
              
        }  
          
          
        public class OnGroupExpandListenerImpl implements OnGroupExpandListener{  
     
            public void onGroupExpand(int groupPosition) {  
                // TODO Auto-generated method stub  
                Toast.makeText(MainActivity.this, "打开分组,groupposition"+groupPosition, Toast.LENGTH_LONG).show();  
            }  
              
        }   
     
          
        @Override  
        public void onCreateContextMenu(ContextMenu menu, View v,  
                ContextMenuInfo menuInfo) {  
            // TODO Auto-generated method stub  
            super.onCreateContextMenu(menu, v, menuInfo);  
              
            ExpandableListView.ExpandableListContextMenuInfo info=  
                    (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;  
              
            int type=ExpandableListView.getPackedPositionType(info.packedPosition);  
            int group=ExpandableListView.getPackedPositionGroup(info.packedPosition);  
            int child=ExpandableListView.getPackedPositionChild(info.packedPosition);  
              
            Toast.makeText(MainActivity.this, "type="+type+";group"+group+";child="+child, Toast.LENGTH_LONG);  
        }  
          
          
          
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            getMenuInflater().inflate(R.menu.activity_main, menu);  
            return true;  
        }  
    }  
    
    

    本文出自 “美好家园” 博客
About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规