ExpandableListView学习

来源:互联网 发布:淘宝模特tim 编辑:程序博客网 时间:2024/05/18 03:44

1、ExpandableListView基本使用(自定义)
  ExpandableListView listview = null;
    ExpandableAdapter exadapter = null;
    List<String> listgroup = null;
    List<List<String>> listparent = null;
    LayoutInflater lf = null;
    private View moreView;//更多按钮


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listgroup = new ArrayList<String>();
        listparent = new ArrayList<List<String>>();


        listview = (ExpandableListView) findViewById(R.id.listview);
        listview.addFooterView(buildFooter());
//取消父列表中的下拉箭头
        listview.setGroupIndicator(null);

        addInfo("griffinshi", new String[] { "13776117119", "man", "Jiangsu", "13776117119", "man", "Jiangsu", "13776117119", "man", "Jiangsu", "13776117119", "man", "Jiangsu", "13776117119", "man", "Jiangsu", "13776117119", "man", "Jiangsu", "man", "Jiangsu" });
        addInfo("lancewu", new String[] { "1321134", "man", "Taiwan" });

        exadapter = new ExpandableAdapter(this);
        listview.setAdapter(exadapter);

//默认自动展开子列表
        int groupCount = exadapter.getGroupCount();
        for (int i=0; i < groupCount; i++) {
            listview.expandGroup(i);
        }
    }

    public void addInfo(String p, String[] c) {
        listgroup.add(p);


        List<String> item = new ArrayList<String>();


        for (int i = 0; i < c.length; i++) {
            item.add(c[i]);
        }


        listparent.add(item);
    }
    
    // 表尾--更多按钮
    protected View buildFooter() {
        LayoutInflater inflater = LayoutInflater.from(this);
        moreView = inflater.inflate(R.layout.listmore, null);
//        moreView.setVisibility(View.GONE);
        Button morebtn = (Button) moreView.findViewById(R.id.morelistbtn);


        morebtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("TEXTNAME", "------more--------");
            }
        });
        
        return moreView;
    }


    public final class NameViewHolder {


        public TextView textname;//日期
    }


    class ExpandableAdapter extends BaseExpandableListAdapter {
        String string = "";
        Context ctxt;
        ImageView imageview2 = null;


        public ExpandableAdapter(Context ctxt) {
            this.ctxt = ctxt;
            lf = LayoutInflater.from(ctxt);
        }


        public Object getChild(int groupPosition, int childPosition) {
            return listparent.get(groupPosition).get(childPosition);
        }


        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }


        public int getChildrenCount(int groupPosition) {
            System.out.println("listparent.size():" + listparent.get(groupPosition).size());
            return listparent.get(groupPosition).size();
        }


//自定义子列表
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
            //            positg = groupPosition;
            //            posititem = childPosition;

            //            LinearLayout linear = (LinearLayout) lf.inflate(R.layout.item, null);
            NameViewHolder nameViewHolder = null;
            if (convertView == null) {
                nameViewHolder = new NameViewHolder();


                convertView = lf.inflate(R.layout.item, null);
                nameViewHolder.textname = (TextView) convertView.findViewById(R.id.itemtextname);
                Log.i("TEXTNAME","-----null----");
            } else {
                nameViewHolder = (NameViewHolder) convertView.getTag();
                Log.i("TEXTNAME","-----not null----");
            }

            nameViewHolder.textname.setTextSize(18);
            nameViewHolder.textname.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            nameViewHolder.textname.setText((String) listparent.get(groupPosition).get(childPosition));


            nameViewHolder.textname.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(ctxt, (String) listparent.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT)
                            .show();
                }
            });

            convertView.setTag(nameViewHolder);

            return convertView;
        }

        // group method stub  
        public Object getGroup(int groupPosition) {
            return listgroup.get(groupPosition);
        }

        public int getGroupCount() {
            return listgroup.size();
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

//自定义父列表
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            lf = LayoutInflater.from(ctxt);
            LinearLayout linear = (LinearLayout) lf.inflate(R.layout.itemgroup, null);

            TextView textname = (TextView) linear.findViewById(R.id.textgroup_name);
            textname.setText(listgroup.get(groupPosition));
            return linear;
        }

        public boolean hasStableIds() {
            return false;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean isEmpty() {

            return false;

        }
    }

Android中替换ExpandableListView控件前面的箭头图标

    首先,自定义一个expandablelistviewselector.xml文件,具体内容如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_expanded="true"android:drawable="@drawable/expandablelistviewindicatordown" />
    <item android:drawable="@drawable/expandablelistviewindicator" />
</selector>
  其次,在程序的主体部分编写类似如下的代码:
 expandList.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector)

关于ExpandableListView未实现的几个功能,请高人指点:
1、子列表很多的时候,滚动子列表,父列表不随子列表滚动。
2、每个子列表如何添加更多按钮,就像父列表的addFooterView一样。

原创粉丝点击