Android ExpandableListView 样式自定义

fmms 12年前

最经搞Android的项目,需要使用ExpandableListView,需要使用XML定制每个选项的样式。具体步骤如下:

  (1) 先用XML 定义 ExpandableListView
       <ExpandableListView           android:id="@+id/widget30"           android:layout_width="317px"               android:layout_height="321px"           android:layout_x="2px"           android:layout_y="70px"           android:drawSelectorOnTop="false">        </ExpandableListView>
 (2) ExpandableListView 需要设置自己的Adapter,通过重写BaseExpandableListAdapter类定制自己的 Adapter 
    public class ExpandableAdapter extends BaseExpandableListAdapter  {      Activity activity;      private List<String> group;        private List<List<String>> child;        public ExpandableAdapter(Activity a)      {         activity = a;       initialData(); //初始化显示的数据      }    @Override  public Object getChild(int groupPosition, int childPosition) {  // TODO Auto-generated method stub  return child.get(groupPosition).get(childPosition);   }  @Override  public long getChildId(int groupPosition, int childPosition) {  // TODO Auto-generated method stub  return childPosition;    }  @Override //设置子选项样式  public View getChildView(int groupPosition, int childPosition,  boolean isLastChild, View convertView, ViewGroup parent) {  // TODO Auto-generated method stub  String string = child.get(groupPosition).get(childPosition);            return getGenericView(string);   }  @Override  public int getChildrenCount(int groupPosition) {  // TODO Auto-generated method stub  return child.get(groupPosition).size();    }  @Override  public Object getGroup(int groupPosition) {  // TODO Auto-generated method stub  return group.get(groupPosition);   }  @Override  public int getGroupCount() {  // TODO Auto-generated method stub  return group.size();    }  @Override  public long getGroupId(int groupPosition) {  // TODO Auto-generated method stub  return groupPosition;    }  @Override //使用LayoutInflater解析定制的XML文件,然后设置相应的样式  public View getGroupView(int groupPosition, boolean isExpanded,  View convertView, ViewGroup parent)   {  // TODO Auto-generated method stub  String string = group.get(groupPosition);   Context mContext = activity;          //LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);       //View layout = inflater.inflate(R.layout.main_listview,null);        LayoutInflater inflater = LayoutInflater.from(mContext);       View layout = inflater.inflate(R.layout.main_listview,null);       TextView my_text = (TextView)layout.findViewById(R.id.ItemTitle);       my_text.setText(string);       //my_text.set       ImageView my_image = (ImageView)layout.findViewById(R.id.ItemImage);       my_image.setImageResource(R.drawable.icon_online);       return layout;          //return getGenericView(string);    }  @Override  public boolean hasStableIds() {  // TODO Auto-generated method stub  return false;  }  @Override  public boolean isChildSelectable(int groupPosition, int childPosition) {  // TODO Auto-generated method stub  return true;  }  public void initialData()  {            group = new ArrayList<String>();                        child = new ArrayList<List<String>>();                        addInfo("Jerry  18718672108", new String[]{"呼叫"});            addInfo("Kim  18718672108",new String[]{"呼叫"});            addInfo("Landy  18718672108",new String[]{"呼叫"});        }                public void addInfo(String p,String[] c)      {            group.add(p);                        List<String> item = new ArrayList<String>();                        for(int i=0;i<c.length;i++){                item.add(c[i]);            }              child.add(item);        }      //View stub to create Group/Children 's View        public TextView getGenericView(String s)       {            // Layout parameters for the ExpandableListView            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                    ViewGroup.LayoutParams.FILL_PARENT, 64);            TextView text = new TextView(activity);            text.setLayoutParams(lp);            // Center the text vertically            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);            // Set the text starting position            text.setPadding(36, 0, 0, 0);                        text.setText(s);            return text;        }        public String GetGroupData(int pos)      {       return group.get(pos);      }  }   
其中关键是使用LayoutInflater解析定制的XML文件,实现了用XML来定制ExpandableListView的组样式。另外,重写了BaseExpandableListAdapter类,实现了自己的Adapter.