要实现一下效果:
1.listview默认展开(expandGroup方法)
2.group不可收回(group的点击事件设为true)
3.去掉group的导航键(android:groupIndicator=“@null”)
4.group点击无效果(编写<Selector>文件,将true和false两种效果设置为同一个背景色,然后再在group的布局中将background属性填进去)。
MainAcitivity:
<pre name="code" class="java"> protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandlv);
elv = (ExpandableListView) findViewById(R.id.elv);
final String[] group = new String[] { "zhang", "wang", "li" };
final String[][] child = new String[][] {
{ "zhangone", "zhangtwo", "zhangsan" },
{ "wangone", "wangtwo", "wangsan" },
{ "lione", "litwo", "lisan" } };
ExpandAdapter adapter = new ExpandAdapter(this, group, child);
int size = adapter.getGroupCount();
Log.d("size", size+"");
elv.setOnGroupClickListener(new OnGroupClickListener() { // 监听group的点击事件
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Log.d("click", "click: " + group[groupPosition]);
return false; //如果返回true,则点击不会收缩
}
});
elv.setOnChildClickListener(new OnChildClickListener() {
// 监听child的点击事件
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("click", "click: " + child[groupPosition][childPosition]);
return false;
}
});
elv.setAdapter(adapter);
for (int i = 0; i < adapter.getGroupCount(); i++) {
elv.expandGroup(i);
Log.d("expand", elv.expandGroup(i)+"");
}
ExpandedAdapter:
package com.example.itemlistener;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandAdapter extends BaseExpandableListAdapter {
private LayoutInflater mInflater;
private String[] group;
private String[][] child;
public ExpandAdapter(Context context, String[] group, String[][] child) {
mInflater = LayoutInflater.from(context);
this.group = group;
this.child = child;
}
/**
* 子控件
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return child[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
convertView= mInflater.inflate(R.layout.item_child, null);
TextView tv=(TextView)convertView.findViewById(R.id.tv_child);
tv.setText(child[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return child.length;
}
/**
* 父控件
*/
@Override
public Object getGroup(int groupPosition) {
return group[groupPosition];
}
@Override
public int getGroupCount() {
return group.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView= mInflater.inflate(R.layout.item_header, null);
TextView tv=(TextView)convertView.findViewById(R.id.tv_group);
tv.setText(group[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() { //这个是判断刷新时是否稳定,具体什么操作我也不清楚
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
Log.d("select", child[groupPosition][childPosition]);
return true;
}
}
Layout_Main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView>
</LinearLayout>
item_header:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_group"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="group" >
</TextView>
</LinearLayout>
item_child:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_child"
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="25dp"
android:text="child" >
</TextView>
</LinearLayout>