我在ExpandableListview
中创建Android
。我使用自定义适配器,但我的所有子项都在每个组中。
我想要的结果:
[ First ] [ Second ] [ Third ]
- AAA - DDD - GGG
- BBB - EEE - HHH
- CCC - FFF - III
(' First',' Second',' Third'是可扩展列表组名称/其他是子项目名称)
但我的清单显示如下:
[ First ] [ Second ] [ Third ]
- AAA - AAA - AAA
- BBB - BBB - BBB
- CCC - CCC - CCC
- DDD - DDD - DDD
- EEE - EEE - EEE
- FFF - FFF - FFF
- GGG - GGG - GGG
- HHH - HHH - HHH
- III - III - III
截图: (我更改了组名)
这是我的代码:
//ExpandableList
private ExpandableListView mPastMedalList;
//ListView Adapter
private ArrayList<String> mGroupList = new ArrayList<>();
private ArrayList<Object> mChildList = null;
private ArrayList<String> mChildListContent = new ArrayList<>();
private void setList(){
setGroupData();
setChildGroupData();
mPastMedalList.setAdapter(new BaseExpandableAdapter(getActivity(), mGroupList, mChildList));
mPastMedalList.expandGroup(0); //default expand
//click event
mPastMedalList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
setExpandableListViewHeight(parent, groupPosition);
return false;
}
});
}
private void setGroupData() {
mGroupList.add("First");
mGroupList.add("Second");
mGroupList.add("Third");
}
private void setChildGroupData() {
mChildList = new ArrayList<>();
mChildListContent.add("AAA");
mChildListContent.add("BBB");
mChildListContent.add("CCC");
mChildList.add(mChildListContent);
mChildList = new ArrayList<>();
mChildListContent.add("DDD");
mChildListContent.add("EEE");
mChildListContent.add("FFF");
mChildList.add(mChildListContent);
mChildList = new ArrayList<>();
mChildListContent.add("GGG");
mChildListContent.add("HHH");
mChildListContent.add("III");
mChildList.add(mChildListContent);
}
和我的适配器来源:
public class BaseExpandableAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groupList = null;
private ArrayList<Object> childList = null;
private LayoutInflater inflater = null;
private ViewHolder viewHolder = null;
public BaseExpandableAdapter(Context c, ArrayList<String> groupList, ArrayList<Object> childList){
super();
this.inflater = LayoutInflater.from(c);
this.groupList = groupList;
this.childList = childList;
}
//return group size
@Override
public int getGroupCount() {
return groupList.size();
}
//return child view size
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childList.get(groupPosition)).size();
}
//return group position
@Override
public String getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
//return child view
@Override
public String getChild(int groupPosition, int childPosition) {
return ((ArrayList<String>)childList.get(groupPosition)).get(childPosition);
}
//return group id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//return child view id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//each group view ROW
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.rio2016_sports_olympic_header, null);
viewHolder.header = (TextView) view.findViewById(R.id.sports_olympic_list_header);
viewHolder.drawer = (ImageView) view.findViewById(R.id.sports_olympic_list_drawer);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)view.getTag();
}
//change icon when open or close
if(isExpanded){ //open
viewHolder.drawer.setImageResource(R.drawable.rio2016_sports_olympic_detail_drawer_up);
}else{ //close
viewHolder.drawer.setImageResource(R.drawable.rio2016_sports_olympic_detail_drawer_down);
}
viewHolder.header.setText(getGroup(groupPosition));
return view;
}
//return child view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.rio2016_sports_olympic_item, null);
viewHolder.medal = (ImageView) view.findViewById(R.id.sports_olympic_list_medal);
viewHolder.flag = (ImageView) view.findViewById(R.id.sports_olympic_list_flag);
viewHolder.countries = (TextView) view.findViewById(R.id.sports_olympic_list_countries);
viewHolder.name = (TextView) view.findViewById(R.id.sports_olympic_list_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.medal.setImageResource(R.drawable.rio2016_sports_olympic_detail_medal);
viewHolder.flag.setImageResource(R.drawable.rio2016_sports_olympic_detail_flag);
viewHolder.countries.setText("KOR");
viewHolder.name.setText(getChild(groupPosition, childPosition));
return view;
}
//
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class ViewHolder {
//header
private TextView header;
private ImageView drawer;
//item
private ImageView medal;
private ImageView flag;
private TextView countries;
private TextView name;
}
}
我想知道为什么每个儿童项目都不正确?请帮我!谢谢,全部:))
答案 0 :(得分:2)
在setChildGroupData()
功能中,您正在使用相同的mChildListContent
变量,而不是为三个变量创建新变量。
你应该这样做 - &gt;
mChildList = new ArrayList<>();
mChildListContent = new ArrayList<>();
mChildListContent.add("AAA");
mChildListContent.add("BBB");
mChildListContent.add("CCC");
mChildList.add(mChildListContent);
mChildList = new ArrayList<>();
mChildListContent = new ArrayList<>();
mChildListContent.add("DDD");
mChildListContent.add("EEE");
mChildListContent.add("FFF");
mChildList.add(mChildListContent);
mChildList = new ArrayList<>();
mChildListContent = new ArrayList<>();
mChildListContent.add("GGG");
mChildListContent.add("HHH");
mChildListContent.add("III");
mChildList.add(mChildListContent);