更改其他子项目的背景时,可扩展的 ListView 子项目会发生变化

Expandable ListView child item get chaneged when changing background of other child item

我有问题,无法理解问题的原因。每当 onChildClick 我更改 Expandable ListViews 子项的背景颜色时,我不仅会更改我选择的项目,还会更改当前子列表和其他子列表中的每九个项目。 我尝试了this, ,还有很多其他的决定,但仍然无法解决。 我做错了什么?任何帮助将不胜感激。

mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){
        @Override
        public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {

view.setBackgroundColor(Color.GREEN);

            mAdapter.notifyDataSetChanged();

            return false;
        }
    });

适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String>muscleGroupName;
private HashMap<String,List<String>>muscleChildName;
private List<Integer>muscleGroupImage;
private HashMap<Integer, List<Integer>>muscleChildImage;

public ExpandableListAdapter(Context context, List<String>muscleGroupName, HashMap<String,List<String>>muscleChildName,
                             List<Integer>muscleGroupImage, HashMap<Integer, List<Integer>>muscleChildImage){
    this.context = context;
    this.muscleGroupName = muscleGroupName;
    this.muscleChildName = muscleChildName;
    this.muscleGroupImage = muscleGroupImage;
    this.muscleChildImage = muscleChildImage;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.muscleChildName.get(this.muscleGroupName.get(groupPosition)).get(childPosition);
}

@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 inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_child, null);
    }
    TextView childNameText = (TextView)convertView.findViewById(R.id.child_name);
    childNameText.setText(childText);

    ImageView childNameImage = (ImageView)convertView.findViewById(R.id.child_image);
    int childImage = this.muscleChildImage.get(this.muscleGroupImage.get(groupPosition)).get(childPosition);
    childNameImage.setImageResource(childImage);

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.muscleChildName.get(muscleGroupName.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return muscleGroupName.get(groupPosition);
}

@Override
public int getGroupCount() {
    return muscleGroupName.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String groupTitle = (String)getGroup(groupPosition);
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_group, null);
    }
    TextView groupNameText = (TextView)convertView.findViewById(R.id.group_name);
    groupNameText.setText(groupTitle);
    ImageView groupImageName = (ImageView)convertView.findViewById(R.id.group_image);
    int imageName = muscleGroupImage.get(groupPosition);
    groupImageName.setImageResource(imageName);
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

如果您只想 select 该项目使用此:

但如果您想自己处理 selection 不要回收转换视图项目并保存 selected 项目位置。像这样的事情:

int selectedPosition=-1;
int selectedPosition_parent=-1;



@Override
public View getChildView(int groupPosition, final int childPosition, boolean   isLastChild, View convertView, ViewGroup parent) {
    final String childText = (String)getChild(groupPosition, childPosition);

    LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.list_child, null);

    TextView childNameText = (TextView)convertView.findViewById(R.id.child_name);
    childNameText.setText(childText);

    ImageView childNameImage = (ImageView)convertView.findViewById(R.id.child_image);
     int childImage = this.muscleChildImage.get(this.muscleGroupImage.get(groupPosition)).get(childPosition);
     childNameImage.setImageResource(childImage);

     if(childPosition==selectedPosition &&  groupPosition==selectedPosition_parent)
         {
           //change the background or anything else
         }


    return convertView;
 }

不要忘记在项目点击或您处理 selection 事件的任何内容中设置 selectedPositionselectedPosition_parent