この例をご覧ください。これがお役に立てば幸いです。
public class ExpandListViewActivity extends ExpandableListActivity
{
/**
* strings for group elements
*/
static final String arrGroupelements[] =
{
"India",
"Australia",
"England",
"South Africa"
};
/**
* strings for child elements
*/
static final String arrChildelements[][] =
{
{
"Sachin Tendulkar",
"Raina",
"Dhoni",
"Yuvi"
},
{
"Ponting",
"Adam Gilchrist",
"Michael Clarke"
},
{
"Andrew Strauss",
"kevin Peterson",
"Nasser Hussain"
},
{
"Graeme Smith",
"AB de villiers",
"Jacques Kallis"
}
};
static final int arrChildelementsim[][] =
{
{
R.drawable.ic_launcher,
R.drawable.aqua,
R.drawable.pin_red,
R.drawable.aqua,
},
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
},
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
},
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
}
};
DisplayMetrics metrics;
int width;
ExpandableListView expList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
expList = getExpandableListView();
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
//this code for adjusting the group indicator into right side of the view
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
expList.setAdapter(new ExpAdapter(this));
expList.setOnGroupExpandListener(new OnGroupExpandListener()
{
@Override
public void onGroupExpand(int groupPosition)
{
Log.e("onGroupExpand", "OK");
}
});
expList.setOnGroupCollapseListener(new OnGroupCollapseListener()
{
@Override
public void onGroupCollapse(int groupPosition)
{
Log.e("onGroupCollapse", "OK");
}
});
expList.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
int arg3, long arg4) {
Log.e("OnChildClickListener", "OK");
// TODO Auto-generated method stub
return false;
}
});
}
public int GetDipsFromPixel(float pixels)
{
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
public class ExpAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpAdapter(Context context) {
myContext = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
ImageView tvPlayerimage = (ImageView) convertView.findViewById(R.id.tvPlayerImage);
tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
tvPlayerimage.setImageResource(arrChildelementsim[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return arrChildelements[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return arrGroupelements.length;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_row, null);
}
TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
tvGroupName.setText(arrGroupelements[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
Child_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/tvPlayerImage"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:gravity="center_vertical"
android:layout_marginLeft="50dip"
>
</ImageView>
<TextView
android:id="@+id/tvPlayerName"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textSize="14sp" >
</TextView>
</LinearLayout>
group_row.xml
<?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" >
<TextView
android:id="@+id/tvGroupName"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:gravity="center_vertical"
android:paddingLeft="30dip"
android:textSize="16sp"
android:textStyle="bold"
>
</TextView>
</LinearLayout>
main.xml
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@drawable/group_indicator" >
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hello" >
</TextView>
</ExpandableListView>
</LinearLayout>