Android ExpandableListView样式定制
来源:互联网 发布:ibm建筑软件 编辑:程序博客网 时间:2024/04/25 22:53
最经搞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(); //初始化显示的数据 }
return getGenericView(string);
//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);
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); }
{
@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);
}
@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;
}
@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()
{
}
其中关键是使用LayoutInflater解析定制的XML文件,实现了用XML来定制ExpandableListView的组样式。另外,重写了BaseExpandableListAdapter类,实现了自己的Adapter.
- Android ExpandableListView样式定制
- Android关于自定义ExpandableListView样式
- [Android]定制RadioButton样式
- Android 定制RadioButton样式
- Android 定制RadioButton样式
- Android 定制RadioButton样式
- Android 定制RadioButton样式
- Android 组件样式定制详解
- Android webkit css样式定制
- Android - ToDoList(定制样式) 详解
- Android Toolbar样式定制详解
- Android控件定制显示样式
- Android Toolbar样式定制详解
- Android Toolbar样式定制详解
- Android Toolbar样式定制详解
- android shape总结 和控件样式定制
- android shape总结 和控件样式定制
- 解读ExpandableListView源码样式
- ListView 的使用
- Oracle10GODP连接11G数据库,出现ORA - 1017用户名/口令无效; 登录被拒绝 的问题
- Active directory改密码后,原密码仍可用的问题
- foursquare -5 BroadcastReceiver 退出程序
- 我的第一个sencha touch应用:移动OA系统
- Android ExpandableListView样式定制
- 如何复制百度文库中的文章
- 指针用法之一:给定结构体求某变量的偏移量,给出某变量偏移量求结构体首地址
- 四. 创建外部表
- J2EE 连接器架构(J2EE Connector Architecture, JCA)简介(一)
- VS2008制作安装包(转)
- 线程安全
- 如何写不同的文档方案
- ACM哈夫曼树建立、哈夫曼编码C++实现