【ExpandableListView】---2 长按和点击处理
来源:互联网 发布:热设计软件 编辑:程序博客网 时间:2024/05/01 20:09
1、 方法一:在创建子项控件的时候对控件整体进行事件的设置
控件的初始化在ExpandableListView的适配器中,在初始化的时候设置点击事件和长按事件
这里只设置了ChildView的点击事件和长按事件
//设置组里面的项的界面@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.child_view, null); view.setClickable(true); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText( MainActivity.this, "子项点击事件", Toast.LENGTH_SHORT).show(); } }); view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText( MainActivity.this, "长按中.......", Toast.LENGTH_SHORT).show(); return true; } });
注意:长按事件中的返回值要是true, 不然执行了长按事件之后还会执行点击事件
2、 方法二:使用ExpandableListView设置子项点击事件进行操作
ExpandableListView 设置OnLongClickListener实现onclick方法,在该方法中并不能比较直观的获取到控件的位置信息。注意下面的获取方式:
//设置item点击的监听器elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText( MainActivity.this, "点击\ngroupPosition=" + groupPosition + ";childPosition=" + childPosition, Toast.LENGTH_SHORT).show(); return false; }});//设置item长按事件elv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { int groupId = -1; int childId = -1; long packedPosition = elv.getExpandableListPosition(position); int type = ExpandableListView.getPackedPositionType(packedPosition); switch (type) { case 0://表示当前选中项为父条目,即Group groupId = ExpandableListView.getPackedPositionGroup(packedPosition); break; case 1://表示当前选中项为子条目 groupId = ExpandableListView.getPackedPositionGroup(packedPosition);//当前子条目所在的Group childId = ExpandableListView.getPackedPositionChild(packedPosition);//当前子条目id break; default: break; } Toast.makeText( MainActivity.this, "长按\nGroupId=" + groupId + ";childId=" + childId, Toast.LENGTH_SHORT).show(); return true; }});
两者的效果是一样的,只是学习阶段,还没有遇到其他问题
效果如下:
demo:
http://download.csdn.net/detail/u012391876/9607876
0 0
- 【ExpandableListView】---2 长按和点击处理
- ExpandableListView实例(一)_数据库增删改查处理和listitem点击长按处理
- 完美解决ExpandableListView长按点击事件
- ExpandableListView的长按点击弹出上下文菜单方法
- 对home键点击和长按的处理
- 自定义ImageView处理其长按和点击事件
- 安卓ExpandableListView长按监听判断点击的item属于哪个group(群组)和child(子项)
- ExpandableListView 长按实现
- ExpandableListView 长按事件
- ExpandableListView长按事件
- ExpandableListView 长按事件
- ExpandableListView长按事件怎么获取groupPosition和childPosition
- 长按ExpandableListView中的选项
- Android ExpandableListView长按事件
- ExpandableListView长按监听实现
- Android学习笔记:如何对ExpandableListView进行精确的点击或长按控制
- 通过标记实现对ExpandableListView的子条目长按点击
- Android长按事件和点击事件问题处理,OnItemLongClickListener和OnItemClickListener冲突问题
- 磁盘缓存的使用DiskLruCache类
- ES5 中的面向对象编程前传 之 深刻理解原型链(prototype)
- flume高并发优化——(11)排除json转换及中文乱码
- Your build settings specify a provisioning profile with the UUID‘XXX’
- JAVA深入研究——Method的Invoke方法。
- 【ExpandableListView】---2 长按和点击处理
- 86. Partition List
- POJ 3468 Lazy标记
- 51nod oj 1272 最大距离【贪心】
- MySQL入门
- Arrayutils数组工具类的学习
- 数据库C语言API编写实例
- HashTable
- cookie到底是什么