ExpendableListView(可展开的列表组件)笔记
来源:互联网 发布:js购物车选中删除 编辑:程序博客网 时间:2024/04/28 17:19
废话少说,直接上代码:
public class ExpendableListViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象,可扩展列表视图适配器
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
//用数组盛放相应图片地址
int [] logos=new int[]{R.drawable.p,
R.drawable.z,R.drawable.t};
//父选项标题
private String[] armTypes=new String[]{
"天堂","地狱","人间"
};
//子选项下的内容
private String[][] arms=new String[][]{
{"小A","小B","小C","小D"},{"中A","中B","中D","中D"},{"大A","大B","大C"}
};
@Override
//判断子列表是否被选中,在相应的图表上可以绑定相应的事件
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
//得到父框架视图
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(ExpendableListViewActivity.this);
ll.setOrientation(0);
ImageView logo=new ImageView(ExpendableListViewActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
@Override
//返回父组件
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypes[groupPosition] ;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
TextView textView=new TextView(ExpendableListViewActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
@Override
//G该方法决定每个子选项 的外观
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取子列表的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
};
ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list);
expandableListView.setAdapter(adapter);
}
}
ExpandableListView思路分:一、父选项的getGroupView 、getGroupId、getGroupCount、getGroup 二,子选项的getChildView、getChile、getChild、getChildrenCount
public class ExpendableListViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象,可扩展列表视图适配器
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
//用数组盛放相应图片地址
int [] logos=new int[]{R.drawable.p,
R.drawable.z,R.drawable.t};
//父选项标题
private String[] armTypes=new String[]{
"天堂","地狱","人间"
};
//子选项下的内容
private String[][] arms=new String[][]{
{"小A","小B","小C","小D"},{"中A","中B","中D","中D"},{"大A","大B","大C"}
};
@Override
//判断子列表是否被选中,在相应的图表上可以绑定相应的事件
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
//得到父框架视图
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(ExpendableListViewActivity.this);
ll.setOrientation(0);
ImageView logo=new ImageView(ExpendableListViewActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
@Override
//返回父组件
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypes[groupPosition] ;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
TextView textView=new TextView(ExpendableListViewActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
@Override
//G该方法决定每个子选项 的外观
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取子列表的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
};
ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list);
expandableListView.setAdapter(adapter);
}
}
ExpandableListView思路分:一、父选项的getGroupView 、getGroupId、getGroupCount、getGroup 二,子选项的getChildView、getChile、getChild、getChildrenCount
- ExpendableListView(可展开的列表组件)笔记
- 可展开的列表组件ExpandableListView
- 可展开的列表组件ExpandableListView
- 可展开的列表组件ExpandableListView
- ExpandableView(可展开的列表组件)
- ExpandableListView-可展开的列表组件
- 可展开的列表组件 ExpandableListview
- ExpandableListView--可展开的列表组件
- Android中可展开的列表组件(ExpandableListView)的使用
- ExpandableListView可展开的列表组件详细解析
- 可展开的列表组件——ExpandableListView简单举例
- Android可展开的列表组件(ExpandableListView)模板
- 可展开的列表组件——ExpandableListView深入解析
- ExpandableListview控件使用---可展开的列表组件
- ExpandableListView(似listview中的item可展开)可展开的列表组件
- Android 可展开列表组件 ExpandableListView
- 可展开列表组件ExpandableListView解析
- ExpandableListView(可展开的列表组件)的说明以及其用法
- objective c学习:协议(protocol)
- SOA的流程完整性(包括交易、信息和交互完整性)
- JS中面向对象编程
- 分页
- MYSQL计划任务介绍
- ExpendableListView(可展开的列表组件)笔记
- ruby入门_类
- :Parencodings
- inux gem install mysql 出错
- Nokia N9 常用终端命令(ls)
- 基于 Android NDK 的学习之旅-----环境搭建
- Android APK反编译详解(附图)
- javascript面向对象1
- nginx hmux模块被冷落了