ExpandInfoAdapter实现二级列表显示

ExpandInfoAdapter实现二级列表显示

By 王军, 2010年04月28日 12:00 上午

android列表,有时需要显示两级列表,使用Listview不能达到很好的效果,可以使用ExpandInfoAdapter很好的实现二级列表,并且能够自定义相应的列表。下面是我定义二级列表的实例:

device

其中的列表布局和相应的图片都是自定义的,实现代码如下:

 

public final static String NAME = "Name:";
    public final static String PHONE = "Phone:";
    public final static String SEX = "Sex:";
    public List<String> group;
    public List<List<String>> child;
    ExpandInfoAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list);
        initialOther();
        addItemByValue("WWWW","051782214","man");
        addItemByValue("ZZZZ","110","mal");
        addItemByValue("DDDD","132","femal");
        addItemByValue("FFFFF","11111","femal");
        adapter = new ExpandInfoAdapter(this);
        expandableListView.setAdapter(adapter);
    }
    public void initialOther(){
        group = new ArrayList<String>();
        child = new ArrayList<List<String>>();
    }
    public void addItemByValue(String n,String c1,String c2){
        group.add(n);
        List<String> item = new ArrayList<String>();
        item.add(NAME+n);
        item.add(PHONE+c1);
        item.add(SEX+c2);
        child.add(item);
    }
    public class ExpandInfoAdapter extends BaseExpandableListAdapter {
        Activity activity;
        public ExpandInfoAdapter(Activity a){
            activity = a;
        }
        @Override
        public Object getChild(int arg0, int arg1) {
            return child.get(arg0).get(arg1);
        }

        @Override
        public long getChildId(int arg0, int arg1) {
            return arg1;
        }
        @Override
        public int getChildrenCount(int arg0) {
            return child.get(arg0).size();
        }

        @Override
        public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
                ViewGroup arg4) {
            View view = arg3;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listview, null);
                LinearLayout layout = (LinearLayout) view
                        .findViewById(R.id.layout_013);
            }
                TextView title = (TextView) view.findViewById(R.id.content_001);
                    title.setText(child.get(arg0).get(arg1));
            return view;
        }
        @Override
        public Object getGroup(int arg0) {
                     return group.get(arg0);
        }

        @Override
        public int getGroupCount() {
                      return group.size();
        }

        @Override
        public long getGroupId(int arg0) {
                    return arg0;
        }

        @Override
        public View getGroupView(int arg0, boolean arg1, View arg2,
                ViewGroup arg3) {
                 View view = arg2;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listview, null);
                LinearLayout layout = (LinearLayout) view
                        .findViewById(R.id.layout_013);
            }
            ImageView imageView=(ImageView)view.findViewById(R.id.tubiao);
           if(arg1)
            {
                imageView.setBackgroundResource(R.drawable.tubiao_button1);
            }else{
                imageView.setBackgroundResource(R.drawable.tubiao_button);
            }
                TextView title = (TextView) view.findViewById(R.id.content_001);
                    title.setText(getGroup(arg0).toString());
            return view;
        }

              public void onGroupExpanded(int groupPosition){
        }
        public void onGroupCollapsed(int groupPosition){
        }
        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }
        @Override
        public boolean hasStableIds() {
            return false;
        }
    }

源代码:http://easymorse-android.googlecode.com/svn/trunk/ExpandableListDemoActivity

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值