我正在使用可扩展的列表视图来填充类别数据。很少有类别具有子类别,而有些则没有。父类别列表显示完美。问题在于子类别。添加的数据是正确的,但是在适配器中呈现数据时,groupId 1中的子类别会添加到具有子类别的其他groupId中,因此groupId子类别会显示在其他类别中。
将数据设置为HashMap时,下面的代码处于活动状态。
private List<String> listDataHeader = new ArrayList<>();
private HashMap<String, List<String>> listDataChild = new
HashMap<String, List<String>>();
private List<String> childValues = new ArrayList<>();
@Override
public void categoriesFetched(List<ArticleCategory> categories) {
listDataHeader.clear();
listDataChild.clear();
childValues.clear();
for (int i = 0; i < categories.size(); i++) {
if (categories.get(i).getSub_categories().size() > 0) {
childValues = new ArrayList<>();
for (int j = 0; j < categories.get(i).getSub_categories().size(); j++) {
childValues.add(categories.get(i).getSub_categories().get(j).getValue());
}
listDataChild.put(categories.get(i).getValue(), childValues);
} else {
listDataChild.put(categories.get(i).getValue(), new ArrayList<String>());
}
listDataHeader.add(categories.get(i).getValue());
}
articlesCategoryAdapter.notifyDataSetChanged();
}
以下代码段是呈现视图时的适配器类。
public class ArticlesCategoryAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader = new ArrayList<>();
private HashMap<String, List<String>> _listDataChild = new HashMap<>();
public ArticlesCategoryAdapter(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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_category_child, null);
String childText = (String) getChild(groupPosition, childPosition);
AppCompatTextView txtListChild = (AppCompatTextView) convertView.findViewById(R.id.txtSubCategoryName);
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 inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_category_group, null);
AppCompatTextView lblListHeader = (AppCompatTextView) convertView.findViewById(R.id.txtMainCategoryName);
lblListHeader.setText(headerTitle);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 0 :(得分:0)
回收视图时未正确设置视图。试试这个:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_category_child, null);
}
AppCompatTextView txtListChild = (AppCompatTextView) convertView.findViewById(R.id.txtSubCategoryName);
txtListChild.setText(childText);
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_category_group, null);
}
AppCompatTextView lblListHeader = (AppCompatTextView) convertView.findViewById(R.id.txtMainCategoryName);
lblListHeader.setText(headerTitle);
return convertView;
}
希望有帮助!