次のような展開可能なリストがあります。
オレンジ色のアイテムは子で、特定の親を押すと表示されます。青の項目は親です。このカスタム アダプターを使用して作成しました: (このソース コードはフォーラムのどこかで選びました)
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の助けを借りて、グループセレクターで機能するこのコードを作成できました。(しかし、2 つのスレッドを使用してセレクターが消える効果を達成するのはちょっとばかげており、アクティビティの静的参照を介してアクセスする必要がありました)
//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で子セレクターを指定する方法はありますか?