尝试折叠嵌套的可扩展列表视图中的所有组时出现问题。
我有一个可扩展的列表视图,每个子节点是另一个可扩展的列表视图。我想要做的是扩展第二个列表中的一个项目以折叠所有其他项目。
m是第一个可扩展的expandablelistview子项。
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
MetrobusStation station = lines.get(groupPosition).getStations()[childPosition];
final MetrobusStationExpandable m = new MetrobusStationExpandable(activity);
MetrobusStationAdapter mAdapter = new MetrobusStationAdapter();
mAdapter.setStation(station);
mAdapter.setLinea(this.lines.get(groupPosition));
mAdapter.setExpandable(m);
m.setGroupIndicator(activity.getResources().getDrawable(R.drawable.arrow_down_w));
activity.setGroupIndicatorToRight(m);
m.setAdapter(mAdapter);
m.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.i("click at child",String.valueOf(childPosition));
parent.expandGroup(groupPosition);
return false;
}
});
exps.put(groupPosition, m);
return m;
}
此代码未记录“点击儿童”,点击时没有输入任何想法?
答案 0 :(得分:0)
我最终做的是一个带有项目的HashMap,并添加了这样的onclick:
private HashMap<String, MetrobusStationExpandable> exps = new HashMap<String, MetrobusStationExpandable>();
MetrobusStation station = lines.get(groupPosition).getStations()[childPosition];
final MetrobusStationExpandable m = new MetrobusStationExpandable(activity);
mAdapter.setStation(station);
mAdapter.setLinea(this.lines.get(groupPosition));
mAdapter.setExpandable(m);m.setGroupIndicator(activity.getResources().getDrawable(R.drawable.arrow_down_w));
m.setAdapter(mAdapter);
m.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
for (String clave: exps.keySet()) {
try { exps.get(clave).collapseGroup(0); }catch(Exception e) {}
}
m.expandGroup(groupPosition);
return true;
}
});
答案 1 :(得分:0)
This one worked for me. Take the import and field instance variable.
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
public class MyDialog extends Dialog implements
OnGroupClickListener{
private int lastExpandedGroupPosition = -1;
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
parent.expandGroup(groupPosition);
if (parent.isGroupExpanded(groupPosition)) {
parent.collapseGroup(groupPosition);
} else {
parent.expandGroup(groupPosition);
}
return false;
}
public void onGroupExpanded(int groupPosition) {
if (groupPosition != lastExpandedGroupPosition) {
expandableListView.collapseGroup(lastExpandedGroupPosition);
}
lastExpandedGroupPosition = groupPosition;
}