• 热门专题

ExpanableListVIew一个奇妙的“手风琴”

作者:wl455624651的专栏  发布日期:2012-01-10 11:01:45
Tag标签:ExpanableListVIew  
  • ExpanableListView是android世界里的神话级别的组件,我个人是这样认为的,几乎在大部分的应用上都能看到它的影子。为了更好的让大家了解什么是ExpanableListView,本人小试牛刀自己写了一个ExpanableListView;话不多说上代码:

    Campsite_expanable_list.java

    import java.util.ArrayList;  
      
    import linkertimes.androidplatform.R;  
    import android.app.Activity;  
    import android.content.Intent;  
    import android.content.res.Resources;  
    import android.graphics.drawable.Drawable;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.ExpandableListView;  
    import android.widget.TextView;  
      
    public class Campsite_expanable_list extends Activity {  
        // 声明listView和adapter;  
        private ExpandableListView elv;  
        private CamAdapter_list adapter;  
          
        // 声明对象;  
        private Button nine_btn;  
        private Button list_btn;  
        private TextView camName_tv;  
          
        // 获取作品图片,获取参加的人数;  
        private ArrayList<Drawable> get_img_drawable_list;  
        private ArrayList<String> get_joinNum_list;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            super.onCreate(savedInstanceState);  
            this.setContentView(R.layout.cam_list);  
              
            // 调用方法进行初始化;  
            init();  
              
            // 获取Intent传过来的营地的信息;  
            Intent in = this.getIntent();  
            String camName_list = in.getStringExtra("camName");  
            camName_tv.setText(camName_list);  
              
            // 获取资源库控制类;  
            Resources res = this.getResources();  
              
              
            get_img_drawable_list.add(res.getDrawable(R.drawable.beauty));  
            get_img_drawable_list.add(res.getDrawable(R.drawable.beauty));  
            get_img_drawable_list.add(res.getDrawable(R.drawable.beauty));  
      
            get_joinNum_list.add(res.getString(R.string.joinNum));  
            get_joinNum_list.add(res.getString(R.string.joinNum));  
            get_joinNum_list.add(res.getString(R.string.joinNum));  
      
            // 初始化adapter;  
            adapter = new CamAdapter_list(this, get_img_drawable_list, get_joinNum_list);  
              
            // 对listView设置;  
            elv.setAdapter(adapter);  
            elv.setGroupIndicator(res.getDrawable(R.drawable.indicator));  
            elv.setIndicatorBounds(300, 320);  
            elv.setDivider(null);  
              
            // 展开listView的所有group分组;  
            expandAllGroups(elv,CamAdapter_list.ACTIVITYNUMS);  
              
            // 设置按钮的监听;  
            nine_btn.setOnClickListener(new OnClickListener() {  
      
                @Override  
                public void onClick(View arg0) {  
                    // TODO Auto-generated method stub  
                    Campsite_expanable_list.this.finish();  
                }  
            });  
        }  
      
        private void init() {  
            // 初始化listView;  
            elv = (ExpandableListView) this.findViewById(R.id.explv);  
              
            // 初始化ArrayList;  
            get_img_drawable_list = new ArrayList<Drawable>();  
            get_joinNum_list = new ArrayList<String>();  
      
            // 初始化按钮;  
            nine_btn = (Button) this.findViewById(R.id.ninego_button_list);  
            list_btn = (Button) this.findViewById(R.id.list_button_list);  
            list_btn.setBackgroundResource(R.drawable.list_pressed);  
            camName_tv = (TextView) this.findViewById(R.id.camName_list);  
        }  
           
        /**  
         * 用于展开所有的listView的group的方法;  
         * @param elv 被操作的对象实例;  
         * @param groupCount elv中所包含的group的个数;  
         */  
        public static void expandAllGroups(ExpandableListView elv,int groupCount){  
            for(int i=0;i<groupCount;i++)  
            {  
                elv.expandGroup(i);  
            }  
        }  
    }  
    


     

    下面是xml布局文件部分:

     cam.list.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" >  
      
        <RelativeLayout  
            android:id="@+id/cam_list"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:background="@drawable/pk_surface_bg" >  
      
            <Button  
                android:id="@+id/newPerson_enterButton_list"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_alignParentLeft="true"  
                android:layout_alignParentTop="true"  
                android:layout_marginLeft="5dp"  
                android:layout_marginTop="5dp"  
                android:background="@drawable/enter_cam_selector" />  
      
            <TextView  
                android:id="@+id/camName_list"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_centerHorizontal="true"  
                android:layout_marginTop="15dp"  
                android:text="@string/fashion_campsite"  
                android:textColor="#960068"  
                android:textSize="20dp" />  
      
            <Button  
                android:id="@+id/ninego_button_list"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_alignBottom="@+id/newPerson_enterButton_list"  
                android:layout_marginLeft="30dp"  
                android:layout_toRightOf="@+id/camName_list"  
                android:background="@drawable/nine_ge" />  
      
            <Button  
                android:id="@+id/list_button_list"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_alignBaseline="@+id/ninego_button"  
                android:layout_alignBottom="@+id/ninego_button_list"  
                android:layout_toRightOf="@+id/ninego_button_list"  
                android:background="@drawable/list" />  
      
            <ExpandableListView  
                android:id="@+id/explv"  
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                android:layout_marginTop="53dp"  
                android:fadingEdge="none"  
                android:scrollbars="none"  
                >  
            </ExpandableListView>  
        </RelativeLayout>  
      
    </RelativeLayout>  
    


     
     

    适配器部分:


    CamAdapter_list.java


     

    import java.util.ArrayList;  
    import java.util.HashMap;  
    import java.util.List;  
    import java.util.Map;  
      
    import linkertimes.androidplatform.R;  
    import android.content.Context;  
    import android.graphics.drawable.Drawable;  
    import android.util.Log;  
    import android.view.LayoutInflater;  
    import android.view.View;  
    import android.view.ViewGroup;  
    import android.widget.BaseExpandableListAdapter;  
    import android.widget.ImageView;  
    import android.widget.TextView;  
      
    public class CamAdapter_list extends BaseExpandableListAdapter {  
      
        public final static int ACTIVITYNUMS = 5;  
        ArrayList<Drawable> img;  
        ArrayList<String> info;  
        Context context;  
        ArrayList<String> groupList;  
        ArrayList<List<Map<String, Object>>> childList;  
        String[] title;  
      
        CamAdapter_list(Context context, ArrayList<Drawable> img,  
                ArrayList<String> info) {  
            this.img = img;  
            this.info = info;  
            this.context = context;  
              
            // 初始化ArrayList;  
            groupList = new ArrayList<String>();  
            childList = new ArrayList<List<Map<String, Object>>>();  
              
            // 初始化一个数组;  
            title = new String[] { "HOT", "New", "Voted", "Joined", "Friend's" };  
              
            // 为ArrayList添加数据;  
            for (int i = 0; i < ACTIVITYNUMS; i++) {  
                groupList.add(title[i]);  
                List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();  
                for (int j = 0; j < img.size(); j++) {  
                    Map<String, Object> map = new HashMap<String, Object>();  
                    children.add(map);  
                    map.put("img", img.get(j));  
                    Log.e("img-->", img.get(j)+"");  
                    map.put("info", info.get(j));  
                }  
                  
                // 与父亲同一层;  
                childList.add(children);  
            }  
        }  
      
        @Override  
        public Object getChild(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
            return childList.get(groupPosition).get(childPosition).get("img");  
        }  
      
        @Override  
        public long getChildId(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
            return childPosition;  
        }  
      
        @Override  
        public View getChildView(int groupPosition, int childPosition, boolean arg2,  
                View convertView, ViewGroup arg4) {  
            // TODO Auto-generated method stub  
            if (convertView == null) {  
                LayoutInflater inflater = LayoutInflater.from(context);  
                convertView = inflater.inflate(R.layout.adpter_child_layout, null);  
            }  
            TextView info = (TextView) convertView.findViewById(R.id.child_tv);  
            info.setText(childList.get(groupPosition).get(childPosition).get("info").toString());  
            ImageView img = (ImageView) convertView.findViewById(R.id.child_img);  
            img.setImageDrawable((Drawable) childList.get(groupPosition).get(childPosition).get("img"));  
            return convertView;  
        }  
      
        @Override  
        public int getChildrenCount(int groupPosition) {  
            // TODO Auto-generated method stub  
            return childList.get(groupPosition).size();  
        }  
      
        @Override  
        public Object getGroup(int groupPosition) {  
            // TODO Auto-generated method stub  
            return groupList.get(groupPosition);  
        }  
      
        @Override  
        public int getGroupCount() {  
            // TODO Auto-generated method stub  
            return groupList.size();  
        }  
      
        @Override  
        public long getGroupId(int groupPosition) {  
            // TODO Auto-generated method stub  
            return groupPosition;  
        }  
      
        @Override  
        public View getGroupView(int groupPosition, boolean arg1, View convertView,  
                ViewGroup arg3) {  
            // TODO Auto-generated method stub  
            if (convertView == null) {  
                LayoutInflater inflater = LayoutInflater.from(context);  
                convertView = inflater.inflate(R.layout.adpter_group_layout, null);  
            }  
            TextView tv = (TextView) convertView.findViewById(R.id.group_tv);  
            tv.setText(groupList.get(groupPosition).toString());  
            return convertView;  
        }  
      
        @Override  
        public boolean hasStableIds() {  
            // TODO Auto-generated method stub  
            return false;  
        }  
      
        @Override  
        public boolean isChildSelectable(int arg0, int arg1) {  
            // TODO Auto-generated method stub  
            return false;  
        }  
      
    }  
    



    以上就是本实例的主要源码,希望对大家的学习有所帮助暂时搁笔了。谢谢大家的支持,如果我写的还好希望大家给我以评论!
     


延伸阅读:

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