知识点:
mElv = (ExpandableListView) this.findViewById(R.id.elv);
Drawable picture = this.getResources().getDrawable(R.drawable.selector);
mElv.setGroupIndicator(picture);//设置前面的小图标
selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_launcher" android:state_expanded="true"/>//下拉时显示
<item android:drawable="@drawable/img3"/>
</selector>
第一步:
建立Xml文件:
<?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="wrap_content" >
</ExpandableListView>
</LinearLayout>
第二步:
建立activity:
package com.example.mylistview;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ExpandableListView mElv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mElv = (ExpandableListView) this.findViewById(R.id.elv);
Drawable picture = this.getResources().getDrawable(
R.drawable.ic_launcher);
mElv.setGroupIndicator(picture);
mElv.setAdapter(new myAdapter());
}
public class myAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO 自动生成的方法存根
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(getApplicationContext(),
R.layout.child, null);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if (groupPosition == 2)
return 3;
return 4;
}
@Override
public Object getGroup(int groupPosition) {
// TODO 自动生成的方法存根
return null;
}
@Override
public int getGroupCount() {
return 5;
}
@Override
public long getGroupId(int groupPosition) {
// TODO 自动生成的方法存根
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = new TextView(getApplicationContext());
tv.setText(" " + groupPosition);
tv.setTextSize(50);
return tv;
}
@Override
public boolean hasStableIds() {
// TODO 自动生成的方法存根
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO 自动生成的方法存根
return true;
}
}