2016-10-01 8 views
0

私は問題があり、その原因を理解できません。 onChildClickがExpandable ListViewsの子アイテムの背景色を変更するときはいつでも、私は選択したアイテムだけでなく、現在の子リストおよび他の子リストのすべての項目を選択します。 私はthisthisを試しましたが、他の多くの決定をしましたが、解決できませんでした。 どうしたのですか?どんな助力も高く評価されます。Expandable ListView子アイテムは他の子アイテムの背景を変更するときにチャレンジされます

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; 
} 
} 

答えて

0

あなただけのこの使用項目を選択する場合: https://stackoverflow.com/a/16190228/6502368

をしかし、あなたが自分で選択を処理したい場合は、ビュー項目を変換して保存リサイクルしないでください選択されたアイテムの位置。このようないくつかの事は:

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; 
} 

項目をクリックするか、何でもあなたが選択イベントを処理中selectedPositionselectedPosition_parentを設定することを忘れないでください。

+0

ありがとう、私はあなたのコードを試してみましたが、私はバックグラウンドカラーですべての缶を得ることはありません。だから私の問題の解決策ではありません。 – Mikhail

+0

複数のアイテムを選択したいのですが、ここから取得すると思います。もう一度、ありがとう! – Mikhail

関連する問題