我有看起来像这样的可扩展列表:
橙色项目是子项,当您按特定父项时显示。蓝色项目是父母。我使用这个自定义适配器来创建这个:(我在论坛的某个地方选择了这个源代码)
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> 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) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, 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) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, 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;
}
}
这些是 xml 文件:
list_item.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="55dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:maxLines="1"
android:background="@drawable/border_orange"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textColor="#f95001"
android:textSize="18sp" />
</LinearLayout>
list_group.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/border"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="#1e90ff"
android:textSize="16sp" />
</LinearLayout>
这就是我指定实际列表的方式:
<ExpandableListView
android:id="@+id/all_songs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:choiceMode="singleChoice"
android:listSelector="@layout/selector_list_item" >
</ExpandableListView>
这是按钮按下的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
<item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
<item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>
</selector>
最近我改变了整个应用程序的外观,在我在列表中的项目上添加这些边框之前一切都很好。但现在我想改变选择器对组项目的行为方式(仍然使用矩形),并且对孩子不同(使用某种椭圆来填充这个橙色椭圆而不是列表项的整个矩形)
我怎样才能做到这一点?我想我可以设法制作带有形状的 xml 可绘制对象以放在选择器上,但是我如何分离父选择器和子选择器?
编辑:如果您需要任何其他源代码,请发表评论。
进步:
在@Omar 的帮助下,我设法创建了适用于组选择器的代码。(但是使用两个线程来实现选择器消失的效果有点愚蠢,我必须通过activity中的静态引用来访问它)
//inside getGroupView() method
if (MusicPlayerActivity.all_songs != null) {
if (isExpanded) {
MusicPlayerActivity.all_songs
.setSelector(R.drawable.child_selector);
} else {
MusicPlayerActivity.all_songs
.setSelector(R.drawable.group_selector);
}
if (MusicPlayerActivity.all_songs != null)
new Thread() {
@Override
public void run() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {e.printStackTrace();
}
if (MusicPlayerActivity.all_songs != null)
MusicPlayerActivity.act
.runOnUiThread(new Runnable() {
@Override
public void run() {
MusicPlayerActivity.all_songs
.setSelector(R.drawable.transparent);
}
});
}
}.start();
}
我还发现可以通过修改选择器 xml 中的第一项来实现这一进展(上图)。那么有没有办法在该xml中为选择器指定子选择器?