0
我想實現一個N級ExpandableListView和我使用這些代碼getChildView
和getGroupView
:N級ExpandableListView
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View view, ViewGroup parent) {
ExpandNode child = (ExpandNode) getChild(groupPosition, childPosition);
ExpandNode group = (ExpandNode) getGroup(groupPosition);
if (group.hasChild()) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.index_expandable_list, null);
}
ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.myExpandableListView);
ExpandAdapter adapter = new ExpandAdapter(context, group.getChilds());
expandableListView.setAdapter(adapter);
} else {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.index_child_row, null);
}
TextView childTextView = (TextView) view.findViewById(R.id.childTextView);
childTextView.setText(child.getTitle().toString());
}
return view;
}
和:
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,ViewGroup parent) {
ExpandNode group = (ExpandNode) getGroup(groupPosition);
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.index_parent_row, null);
}
TextView childTextView = (TextView) view.findViewById(R.id.parentTextView);
childTextView.setText(group.getTitle().toString());
return view;
}
問題是當我使用嵌套適配器,如getChildView
中所見,groupPosition
值在內部適配器的getGroupView
中始終爲0,並始終引用arrayList中的第一個值。 例如: ArrayList中的值是:
Level1
level1_1
level1_2
level1_3
level2
但它表明:
Level1
level1_1
level1_1
level1_1
level2
它,當我使用嵌套適配器只發生,它使用getChildView
這樣當工作原理:
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View view, ViewGroup parent) {
ExpandNode child = (ExpandNode) getChild(groupPosition, childPosition);
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.index_child_row, null);
}
TextView childTextView = (TextView) view.findViewById(R.id.childTextView);
childTextView.setText(child.getTitle().toString());
return view;
}
但它不再是N級別的列表... 有沒有一個建議我該如何解決這個問題?在我的適配器
其他實施方法如下所述:
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChilds().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).getChilds().size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
你解決了你的問題嗎? – Jilberta
不好,不幸的是... –
所以你沒有像TreeView一樣編寫N級ExpandableListView? – Jilberta