如果我理解正确,您希望在 ExpandableListView 的每个父行中都有一个复选框。我之前做过类似的事情(不是使用复选框,而是使用按钮),我必须实现一个自定义适配器。应该是这样的:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListTest extends BaseExpandableListAdapter {
private Context context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListTest(Context context) {
this.context = context;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
if (_listDataChild != null) {
return this._listDataChild.get(
this._listDataHeader.get(groupPosition))
.get(childPosititon);
} else {
return null;
}
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int position = groupPosition;
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.text);
txtListChild.setText(childText);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// do something
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if (_listDataChild == null) {
return 0;
} else {
return this._listDataChild.get(
this._listDataHeader.get(groupPosition)).size();
}
}
@Override
public Object getGroup(int groupPosition) {
if (_listDataHeader == null) {
return null;
} else {
return this._listDataHeader.get(groupPosition);
}
}
@Override
public int getGroupCount() {
if (_listDataHeader == null) {
return 1;
} else {
return this._listDataHeader.size();
}
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int pos, boolean isExpanded, View view,
ViewGroup parent) {
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.layout_with_checkbox, null);
}
getGroup(pos);
CheckBox checkBox=(CheckBox)view.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg1){
//the checkBox is checked
}
}
});
return view;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
如果您想将复选框放在子行中,只需切换 getGroupView() 和 getChildView() 中使用的代码和布局即可。
父布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<CheckBox
android:id="@+id/checkBox"
android:layout_width="30dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Text goes here"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>