我有一个由两个自定义对象组成的自定义可扩展列表视图。我希望对列表的子项具有单击效果,以便用户知道他们正在单击哪个项目。问题是,在创建自定义适配器后,我无法获得点击效果。我在drawable文件夹中有一个选择器,我将它分配给我的可扩展列表视图,因为某些原因它无法正常工作。任何人都可以帮我解决这个问题。感谢
main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.grr.MainActivity"
tools:ignore="MergeRootFrame" >
<ExpandableListView
android:id="@+id/expandableListView1"
android:choiceMode="singleChoice"
android:layout_width="match_parent"
android:listSelector="@drawable/list_selector"
android:layout_height="wrap_content" >
</ExpandableListView>
</FrameLayout>
和我的 selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/black"
android:state_pressed="true" />
<item android:drawable="@android:color/darker_gray"
android:state_checked="true" />
<item android:drawable="@android:color/white"
android:state_selected="true" />
</selector>
这是我的 CustomAdapter 类
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<MainBodyArea> _listDataHeader;
private HashMap<MainBodyArea, List<SubBodyArea>> _listDataChild;
public ExpandableListAdapter(Context context, List<MainBodyArea> listDataHeader,
HashMap<MainBodyArea, List<SubBodyArea>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
SubBodyArea temp = this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
return temp.getID();
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
SubBodyArea temp = (SubBodyArea) getChild(groupPosition, childPosition);
final String childText = temp.getName();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_child, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
MainBodyArea temp = (MainBodyArea) getGroup(groupPosition);
String headerTitle = temp.getName();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_header, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 0 :(得分:0)
这很容易
在list_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="wrap_content"
android:background="@drawable/selector"
android:orientation="vertical">
<!-- your rest of layout -->
.............
.............
<!-- your rest of layout -->
</LinearLayout>