android中经常用到的 ExpandableListView 的简单使用
来源:互联网 发布:tigervnc ubuntu 编辑:程序博客网 时间:2024/06/05 18:58
代码也很简单,直接上代码啦。注释比较详细。
首先是 xml, groupitem 和 childitems 就不贴了 这个自己随便吧。
main.xml 很简单,就放了个 ExpandableListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ExpandableListView android:id="@+id/fragment_aa" android:layout_width="match_parent" android:layout_height="match_parent" /></RelativeLayout>
接下来 activity中的设置
public class MainActivity extends Activity {private ExpandableListView ll;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ll =(ExpandableListView)this.findViewById(R.id.fragment_aa);/** 造的假数据 让能够显示 * */List<GroupEntity> group_list = new ArrayList<GroupEntity>();//孩子集合List<List<ChildEntity>> chis = new ArrayList<List<ChildEntity>>();for (int i = 0; i < 3 ; i++) {List<ChildEntity> chile_list = new ArrayList<ChildEntity>();ChildEntity chile = new ChildEntity();chile.name = String.valueOf(i+"chile- name");chile.age = String.valueOf(i+"child -age");GroupEntity group = new GroupEntity();group.groupName = String.valueOf(i+"group - name");chile_list.add(chile);group_list.add(group);chis.add(chile_list);if( i == 2){List<ChildEntity> chile_list1 = new ArrayList<ChildEntity>();ChildEntity chile1 = new ChildEntity();chile1.name = String.valueOf(i+"chile- name");chile1.age = String.valueOf(i+"child -age");GroupEntity group1 = new GroupEntity();group1.groupName = String.valueOf(i+"group - name");chile_list.add(chile1);chis.add(chile_list1);}} //----- 上面造假数据 -------------------------------------------------------------------// 初始化适配器ExpandInfoAdapter1 adapter1 = new ExpandInfoAdapter1(this, group_list,chis);//设置背景ll.setBackgroundResource(R.drawable.online_bj);//指示器ll.setChildIndicator(getResources().getDrawable(R.drawable.ic_launcher));ll.setIndicatorBounds(150, 0);//group 用自己的指示器 设置默认展开哪一行ll.setGroupIndicator(null);ll.expandGroup(0);//listView.setGroupIndicator(getResources().getDrawable(R.drawable.arrow_list2));ll.setAdapter(adapter1);/** * 提供的 */ll.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {// TODO Auto-generated method stubExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; int type = ExpandableListView.getPackedPositionType(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP ) { menu.add(1, 0, 0, "地图查看" ); menu.add(2, 0, 1, "数据查看" ); }}});/** * 自己实现的样子当长按时 *///ll.setOnItemLongClickListener(new OnItemLongClickListener() {////@Override//public boolean onItemLongClick(AdapterView<?> arg0, View view,//int position, long id) {//Toast.makeText(MainActivity.this, "position=="+position+"==id=="+ id, 1).show();////return true;////////}////});}//点击事件 处理@Overridepublic boolean onContextItemSelected(MenuItem item) {if (item.getGroupId() < 3) {ExpandableListView.ExpandableListContextMenuInfo info_del =(ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();switch (item.getOrder()) {case 0:Intent it = new Intent();Toast.makeText(MainActivity.this, "position==0", 1).show();break;case 1:Toast.makeText(MainActivity.this, "position==1", 1).show();break;default:break;}}return super.onContextItemSelected(item);}}
接下来是 适配器:
public class ExpandInfoAdapter1 extends BaseExpandableListAdapter {private List<GroupEntity> group_list; //组private List<List<ChildEntity>> child_list; //childprivate LayoutInflater mLayoutInflater;private Context context;public ExpandInfoAdapter1(Context mContext, List<GroupEntity> group_list, List<List<ChildEntity>> child_list) {this.context = mContext;this.group_list =group_list;this.child_list = child_list;this.mLayoutInflater = LayoutInflater.from(mContext);}//得到每个“组”下面对应的 有多少条数据@Overridepublic Object getChild(int groupPosition, int childPosition) {return child_list.get(groupPosition).get(groupPosition);}@Overridepublic long getChildId(int groupID, int groupPosition) {return 0;}//子界面布局@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {//加载子布局界面if (convertView == null) {convertView = mLayoutInflater.inflate(R.layout.listview_child_item,null);} //得到子布局信息final ChildEntity childEntity = child_list.get(groupPosition).get(childPosition);LinearLayout groupItem = (LinearLayout) convertView.findViewById(R.id.ll_child_item);// nameTextView name= (TextView) convertView.findViewById(R.id.tv_name);name.setText(childEntity.name);//ageTextView age= (TextView) convertView.findViewById(R.id.tv_age);age.setText(childEntity.age);//convertView.setTag(R.id.ll_SO2, groupPosition);return convertView;}//每个group view 下面显示多少个 chile view@Overridepublic int getChildrenCount(int groupID) {return child_list.get(groupID).size();}//得到每个组@Overridepublic Object getGroup(int groupPosition) {return group_list.get(groupPosition);} //多少个组@Overridepublic int getGroupCount() {return group_list.size();}@Overridepublic long getGroupId(int groupID) {return 0;}/** * group */@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stubif (convertView == null) {convertView = mLayoutInflater.inflate(R.layout.listview_group_item,null);}GroupEntity model = group_list.get(groupPosition);//得到linearlayoutLinearLayout groupItem = (LinearLayout) convertView.findViewById(R.id.ll_group_itme);//groupItem.setTag(R.id.ll_group_itme, 1);//设置groupTextView groupName = (TextView) convertView.findViewById(R.id.tv_name);groupName.setText(model.groupName);//自己设置的指示器ImageView image = (ImageView)convertView.findViewById(R.id.adapter_listview_group_image);if (isExpanded) { //展开时显示的图片image.setBackgroundResource(R.drawable.widget_expander_ic_maximized);}else{//收起时显示的图片image.setBackgroundResource(R.drawable.widget_expander_ic_minimized);}return convertView;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}
或者设置指示器 写到 drawable 也是可以的,就不用写在 adapter里面了。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_expanded="true" android:drawable="@drawable/ic_launcher" /> <item android:drawable="@drawable/ic_launcher"></item> </selector>
再添加1个 工具类,
这个的用处就是 当你直接有 组List <entity> 和 子 List<entity> 不用再次封装 成 子 List<List<entity>>了直接传入,在adapter中使用就好了。
public class SetExpandGroupArray{private List<GroupEntity> group_list;private List<ChildEntity> child_list;public SetExpandGroupArray(List<GroupEntity> group_list, List<ChildEntity> child_list){this.group_list = group_list;this.child_list = child_list;}//取出 group_list 每个组头信息public GroupEntity getGroupEntity(int groupID){return groupID < group_list.size() ? group_list.get(groupID) : null;}//copyprivate void copy(List<ChildEntity> list_child, List<ChildEntity> res, int start, int end, int index){list_child.add(res.get(index));} //根据 group position child postion 返回相应的数据public ChildEntity getChild(int groupID, int groupPosition){ChildEntity selectedContact = getGroup(groupID).get(groupPosition);return selectedContact != null ? selectedContact : null;}//根据组 ID 返回 child_listpublic List<ChildEntity> getGroup(int groupID){if (!(groupID < group_list.size())){return null;} else{List<ChildEntity> contactGroup = new ArrayList<ChildEntity>();copy(contactGroup, child_list, 0, 0,groupID);return contactGroup;}}public int getGroupCount(){return group_list.size();} //返回每个组下 的 数据的 sizepublic int getChildrenCountgetChildrenCount(int groupID){return getGroup(groupID).size();}}
adapter类中的 参数把上面的工具类(已经塞入过 组list数据 和 子list数据) 对象传入就好了。可以直接用里面的方法了
public ExpandInfoAdapter(Context mContext, GetAirStationDateGroupArray list) {this.context = mContext;this.mCitySiteEntryGroupArray = list;this.mLayoutInflater = LayoutInflater.from(mContext);}
- android中经常用到的 ExpandableListView 的简单使用
- 在android中,经常用到的定时器
- Android开发中经常用到的工具
- 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的使用
- 黑马程序员--静态导入
- 嵌入式Linux之我行——ARM MMU工作原理剖析
- ios7新特性--9
- overload与override
- C++设计模式之十二--Bridge桥梁模式
- android中经常用到的 ExpandableListView 的简单使用
- MongoDB安装(Win7)
- Android中图片占用内存的计算
- hdu 1217
- 黑马程序员--关于Eclipse
- BeagleBoard Black A5C boot log
- 黑马程序员:集合类:List(ArrayList、LinkedList、Vector)、Set(HashSet、TreeSet)介绍
- (阶段三 dijkstra算法温习1.1)HDU 2544 最短路(利用dijkstra算法计算两个节点间的最短路径)
- JavaScript学习篇之——初始JavaScript