ExpandableListView 使用及设置监听函数
2016-04-27 22:36
435 查看
直接贴代码(原谅中二的我)
注意getChildView和getGroupView中的view声明必须在内部,相当于每次创建表项都new一个view来显示,否则会后只是会显示最后一个group和最后一个child。
expandablelistview:
childview
groupview:
其中几个重要的方法
1、getGroupCount()
返回包含的组列表项的数量。
2、getGroupView()
该方法将返回的View对象将作为组列表项
3、getChildCount()
该方法返回特定组所包含的子列表项的数量
4、getChildView()
该方法返回的View对象将作为特定组、特定位置的子列表项
5、isChildSelectable()
该方法返回true时,可以对子列表项设置监听
监听函数
1、listView.setOnChildClickListener()
监听子列表项按下事件
2、setOnGroupClickListener()
监听组列表项按下事件
注意:setOnGroupClickListener()返回值必须为false时,才可以正确监听(但是并没搞懂为什么)
这是OnGroupClickListener的api介绍
onChildClick
boolean onChildClick(ExpandableListView parent,
View v,
int groupPosition,
int childPosition,
long id)
Callback method to be invoked when a child in this expandable list hasbeen clicked.
参数:
parent - The ExpandableListView where the click happened
v - The view within the expandable list/ListView that was clicked
groupPosition - The group position that contains the child thatwas clicked
childPosition - The child position within the group
id - The row id of the child that was clicked
返回:
True if the click was handled
但是最后意思是 如果事件被处理返回true,有点搞不懂。
public class MyExpandableListView extends Activity{ private ExpandableListView listView; private TextView childView,groupView; private ImageView imageView; private LinearLayout linearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandablelistview); initview(); listView = (ExpandableListView)findViewById(R.id.expandablelistview); linearLayout = (LinearLayout)findViewById(R.id.layout); final int[] img = new int[]{ R.mipmap.a, R.mipmap.b, R.mipmap.c }; final String[] title = new String[]{ "物语系列","FATE ZERO","钢之炼金术" }; final String[][] str = new String[][]{ {"阿拉拉垃圾君","战场原","八九寺"}, {"卫宫切嗣","saber","金闪闪"}, {"艾德","艾尔","大佐"} }; ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter() { @Override public void registerDataSetObserver(DataSetObserver observer) { } @Override public void unregisterDataSetObserver(DataSetObserver observer) { } @Override public int getGroupCount() { return title.length; } @Override public int getChildrenCount(int groupPosition) { return str[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return title; } @Override public Object getChild(int groupPosition, int childPosition) { return str[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final View view2 = LayoutInflater.from(MyExpandableListView.this).inflate(R.layout.groupview,null); groupView = (TextView)view2.findViewById(R.id.groupview); imageView = (ImageView)view2.findViewById(R.id.imageview); imageView.setImageResource(img[groupPosition]); groupView.setText(title[groupPosition]); return view2; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view1 = LayoutInflater.from(MyExpandableListView.this).inflate(R.layout.childview,null); childView = (TextView)view1.findViewById(R.id.childview); childView.setText(str[groupPosition][childPosition]); return childView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean areAllItemsEnabled() { return true; } @Override public boolean isEmpty() { return false; } @Override public void onGroupExpanded(int groupPosition) { } @Override public void onGroupCollapsed(int groupPosition) { } @Override public long getCombinedChildId(long groupId, long childId) { return 0; } @Override public long getCombinedGroupId(long groupId) { return 0; } }; listView.setAdapter(expandableListAdapter); listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Log.i(""+MyExpandableListView.this, "group " + groupPosition + " child " + childPosition); return false; } }); listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { Log.i("" + MyExpandableListView.this, "group " + groupPosition ); return false; } }); } }
注意getChildView和getGroupView中的view声明必须在内部,相当于每次创建表项都new一个view来显示,否则会后只是会显示最后一个group和最后一个child。
expandablelistview:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="match_parent"></ExpandableListView> </LinearLayout>
childview
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/childview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
groupview:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageview" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/groupview" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout>
其中几个重要的方法
1、getGroupCount()
返回包含的组列表项的数量。
2、getGroupView()
该方法将返回的View对象将作为组列表项
3、getChildCount()
该方法返回特定组所包含的子列表项的数量
4、getChildView()
该方法返回的View对象将作为特定组、特定位置的子列表项
5、isChildSelectable()
该方法返回true时,可以对子列表项设置监听
监听函数
1、listView.setOnChildClickListener()
监听子列表项按下事件
2、setOnGroupClickListener()
监听组列表项按下事件
注意:setOnGroupClickListener()返回值必须为false时,才可以正确监听(但是并没搞懂为什么)
这是OnGroupClickListener的api介绍
onChildClick
boolean onChildClick(ExpandableListView parent,
View v,
int groupPosition,
int childPosition,
long id)
Callback method to be invoked when a child in this expandable list hasbeen clicked.
参数:
parent - The ExpandableListView where the click happened
v - The view within the expandable list/ListView that was clicked
groupPosition - The group position that contains the child thatwas clicked
childPosition - The child position within the group
id - The row id of the child that was clicked
返回:
True if the click was handled
但是最后意思是 如果事件被处理返回true,有点搞不懂。
相关文章推荐
- C++实验4-作业
- Objective-C中的block代码块的使用
- Linux下安装JDK、Tomcat以及与Apache整合
- AVL在计算机科学中是最先发明的自平衡二叉查找树
- linux基本命令(7)——mv命令
- Six Basic Functional Areas of Spring
- Android原生控件---ActionBar详解
- SQL-INSERT-插入数据
- 关于继承及父类引用指向子类的一些总结
- iOS x264 编译
- 2.Samba安装问题:error while loading shared libraries: libXXX.so.X: cannot open shared object file
- TCL通讯web前端电话面试
- 对于超体的一点思考
- python day1:初识Python(一)
- ldpack工作日记-2016/4/27
- 关于VMware上Linux克隆后网卡名称修改的操作
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- java并发之Condtion在生产者消费者中的应用
- 字符串与json之间的相互转化
- IIS not allow PUT and DELETE method