Android之ExpandableListView
来源:互联网 发布:C语言 函数库 下载 编辑:程序博客网 时间:2024/06/06 08:44
ExpandableListView笔记
ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew显示的列表项应该由ExpandableAdapter提供。
ExpandableListVivew的代码实现:
在XML文件:
先建一个ExpandableListView XML文件
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg"
>
<ExpandableListView
android:id="@+id/main_expandableListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
再建一个ExpandableAdapter元素的XML文件
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/main_activity_account_layout_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:textSize="8pt"/>
</LinearLayout>
在java文件中:
privateExpandableListViewexpandListView;
private ExpandableListAdapteradapter;
private ExpandableListContextMenuInfoinfo;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**实例化ExpandableListView对象*/ expandListView=(ExpandableListView)findViewById(R.id.main_expandableListView);
/**一个baseExpandableListAdapter适配器*/
adapter=new BaseExpandableListAdapter() {
@Override
publicboolean isChildSelectable(int groupPosition, int childPosition) {
returntrue;
}
@Override
publicboolean hasStableIds() {
returntrue;
}
@Override/**获得groupView*/
public View getGroupView(finalint groupPosition, boolean isExpanded,View convertView,ViewGroup parent) {
LayoutInflaterinflater=LayoutInflater.from(context);
View view=
inflater.inflate(R.layout.main_activity_category_layout,null);
return view;
}
@Override
publiclong getGroupId(int groupPosition) {
return groupPosition;
}
@Override
publicint getGroupCount() {
returnlist_category.size();
}
@Override
public Object getGroup(int groupPosition) {
Category cg=list_category.get(groupPosition);
return cg.getName();
}
@Override
publicint getChildrenCount(int groupPosition) {
Category cg=list_category.get(groupPosition);
list_account=cdao.deepList_LoadAccountName(context, cg.getId());
returnlist_account.size();
}
@Override/**获得chileView*/
public View getChildView(int groupPosition,int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.main_activiyt_account_layout,null);
TextView textview=(TextView)view.findViewById(R.id.main_activity_account_layout_textview);
return view;
}
@Override
publiclong getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public Object getChild(int groupPosition,int childPosition) {
Category cg=list_category.get(groupPosition);
list_account=cdao.deepList_LoadAccountName(context,cg.getId());
Account ac=list_account.get(childPosition);
return ac.getName();
}
@Override
publicvoid notifyDataSetChanged() {
super.notifyDataSetChanged();
}
};
expandListView.setAdapter(adapter);
//注册一个上下文菜单
registerForContextMenu(expandListView);
//对childView进行点击监听
expandListView.setOnChildClickListener(new
ExpandableListView.OnChildClickListener(){
@Override
publicboolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition,long id) {
returntrue;
}
});
}
@Override
publicvoid onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
/**获得长按对象的所有信息*/
info=(ExpandableListContextMenuInfo)menuInfo;
// 得到父位置和子位置
System.out.println(ExpandableListView.getPackedPositionGroup(info.packedPosition)+"->father");
System.out.println(ExpandableListView.getPackedPositionChild(info.packedPosition)+"->child");
/**获得长按的类型,父型或子型*/
inttype=ExpandableListView.getPackedPositionType(info.packedPosition);
refreshCategoryData();
if(type==ExpandableListView.PACKED_POSITION_TYPE_GROUP){ menu.add(1,CATEGORY_UPDATE,1,R.string.category_update_title);
menu.add(1,CATEGORY_DELETE,2,R.string.category_delete_title);
}elseif(type==ExpandableListView.PACKED_POSITION_TYPE_CHILD){
menu.add(1,ACCOUNT_UPDATE,1,R.string.account_update_title);
menu.add(1,ACCOUNT_DELETE,2,R.string.account_delete_title);
}
super.onCreateContextMenu(menu, v, menuInfo);
}
Android中替换ExpandableListView控件前面的箭头图标
首先,自定义一个expandablelistviewselector.xml文件放到layout,具体内容如下:
<?xmlversion="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true"
//down存放已有下拉时的图片
android:drawable="@drawable/expandablelistviewindicatordown"/>
//存放还没有下拉时的图片
<item android:drawable="@drawable/expandablelistviewindicator"/>
</selector>
其次,在程序的主体部分编写类似如下的代码:
expandList.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));
解决ExpandableList滑动时出现黑屏的情况:
expandList.setCacheColorHint(Color.TRANSPARENT);
- Android控件之ExpandableListView
- Android之ExpandableListView
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- Android开发之ExpandableListView
- Android控件之ExpandableListView
- android之ExpandableListView
- Android控件之ExpandableListView
- Android开发之ExpandableListView
- Android之ExpandableListView
- Android之ExpandableListView
- Android之ExpandableListView
- Android开发之ExpandableListView
- Android开发之ExpandableListView
- Android 之ExpandableListView使用
- Android开发之ExpandableListView
- android 学习之ExpandableListView
- Android之ExpandableListView
- 使用一组radio button
- 九度1077 最大序列和
- 多线程编程模型
- Adobe AIR教程:iOS应用内付费简介和准备流程
- 整理综合之一 ----从Bytecode到BasicBlock
- Android之ExpandableListView
- 设计模式(十六)----- 迭代器模式(Iterator)----(JAVA版)
- Who do you think is the best coder, and why?
- HTML001——meta标签
- Android之sensor 传感器
- 安全卫士第四天--23黑名单拦截
- 1022 Train Problem 1 栈的运用
- 面相对象设计中组合与聚合详解
- 原创分析:iOS 中使用 Mobile Installation 安装 IPA(使用iTunes/AppStore一样的安装API)