注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

是缘是情是童真

如果你用笑脸面对 你会看见别人的笑脸

 
 
 

日志

 
 

android:ExpandableListView简单使用  

2011-11-04 11:43:12|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
这个控件比较让人讨厌~~
界面:
android:ExpandableListView简单使用 - 只要心里有爱 - 只要心里有爱
 

这里发个简单的代码。。。

public class ExpandableListProActivity extends Activity {
private ExpandableListView expand;
private ExpandableListAdapter adapter;
private List<String> group = new ArrayList<String>();
private List<List<String>> child = new ArrayList<List<String>>();
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        expand = (ExpandableListView) findViewById(R.id.list);
        init();
        
        adapter = new ExpandableListAdapter(this,group,child);
        expand.setAdapter(adapter);


        expand.setOnGroupClickListener(new OnGroupClickListener(){

public boolean onGroupClick(ExpandableListView arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(ExpandableListProActivity.this,"[Group Click]:"+arg2,Toast.LENGTH_LONG).show();
return false;
}
       
        });
        
        expand.setOnChildClickListener(new OnChildClickListener(){

public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) {
// TODO Auto-generated method stub
Toast.makeText(ExpandableListProActivity.this,"[Child Click]:"+arg2+":"+arg3,Toast.LENGTH_LONG).show();
return false;
}
       
        });
    }
public void init(){
    group = new ArrayList<String>();
   
    child = new ArrayList<List<String>>();
   
    addInfo("第一项", new String[]{"13776117119","man","Jiangsu"});
    addInfo("第二项",new String[]{"1321134","man","Taiwan"});
    addInfo("第三项",new String[]{"12345"});
   }
   
   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);
   }
}

//adapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> group;
private List<List<String>> child;
public ExpandableListAdapter(Context context) {
super();
this.context = context;
}
public ExpandableListAdapter(Context context, List<String> group,
List<List<String>> child) {
super();
this.context = context;
this.group = group;
this.child = child;
}

@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) {
System.out.println("getChildView");
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
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
System.out.println("getGroupView");
String string = group.get(groupPosition);
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;
}
//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(context);
        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;
    }

}

  评论这张
 
阅读(736)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017