public class MainActivity extends Activity {
private List<String> group;
private List<List<String>> child;
private ExpandableListAdapter adapter;
private TextView timeView,durView,delView;
private ImageView image;
private Handler handler;
private int cnt=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new Handler();
group = new ArrayList<String>();
child = new ArrayList<List<String>>();
String g[]= { "test1", "test2", "test3","test3","test3" };
String c[][] = {{ "aaa" },{ "a","b","c" },{},
{ "111","222" ,"333","111","333","111","222" ,"333"},{}};
addgroup( g );
addchild( c );
adapter = new BaseExpandableListAdapter() {
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (null == convertView || !(convertView instanceof RelativeLayout)) {
convertView = LayoutInflater.from(MainActivity.this).inflate(
R.layout.item, null);
if(childPosition == 0){
Log.d("tag", "group "+groupPosition+" first click");
}
}
image = (ImageView) convertView.findViewById(R.id.image);
timeView = (TextView) convertView.findViewById(R.id.title);
durView = (TextView) convertView.findViewById(R.id.info);
delView = (TextView) convertView.findViewById(R.id.delete);
Log.d("tag", "group="+child.size()+",gp="+groupPosition);
Log.d("tag", "child="+child.get(groupPosition).size()+",cp="+childPosition);
if((child.size() > groupPosition)&&(child.get(groupPosition).size() > childPosition)){
timeView.setText(child.get(groupPosition).get(childPosition).toString());
durView.setText("test123456");
timeView.setVisibility(View.VISIBLE);
durView.setVisibility(View.VISIBLE);
delView.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}else {
timeView.setVisibility(View.GONE);
durView.setVisibility(View.GONE);
delView.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "group:"+groupPosition
+",child:"+childPosition, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(MainActivity.this);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
public TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 100);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(80, 0, 0, 0);
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
};
ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list);
expandListView.setAdapter(adapter);
// test();
}
private void test(){
new Timer().schedule(new TimerTask() {
public void run() {
cnt=cnt++>20?0:cnt;
modifyChildVal(1, cnt);
modifyGroupVal(0, cnt);
Runnable updater = new Runnable() {
public void run() {
((BaseExpandableListAdapter) adapter).notifyDataSetChanged();
}
};
handler.post(updater);
};
}, 0, 3000);
}
public void addgroup(String[] c) {
for (int i = 0; i < c.length; i++) {
group.add(c[i]);
}
}
public void addchild(String[][] c) {
for (int i = 0; i < c.length; i++) {
List<String> tmpList = new ArrayList<String>();
for (int j = 0; j < c[i].length; j++) {
tmpList.add(c[i][j]);
}
child.add(tmpList);
}
}
public void modifyChildVal(int index, int s) {
List<String> tmpList = child.get(index);
String tmp = s+"";
tmpList.set(0, tmp);
child.set(index, tmpList);
}
public void modifyGroupVal(int index, int s) {
String tmp = "abc"+s;
group.set(index, tmp);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ExpandableListView>
</LinearLayout>
列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:src="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666872" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666872"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right" >
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_margin="12dp"
android:text="删除" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
效果: