4

我想更改在 ExpandableListView 中单击的子项的背景颜色。也就是说,当点击任何孩子时,它的背景颜色应该会改变。我正在尝试以这种方式进行操作,但它选择了其他行,而不是已单击的行。

public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    parent.getChildAt(childPosition).setBackgroundColor(Color.DKGRAY);

    return false;
}

请告诉我我可能会错过什么。

4

3 回答 3

6

这就是我解决它的方法。

View _lastColored;
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    _groupPosition = groupPosition;


    if(_lastColored != null)
    {
    _lastColored.setBackgroundColor(Color.TRANSPARENT);
    _lastColored.invalidate();
    }
    _lastColored = v;
    v.setBackgroundColor(Color.rgb(214, 214, 214));


    return false;
}
于 2012-06-16T14:42:21.970 回答
3

假设您尝试直接引用您的视图并像这样设置背景怎么样,

public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

         v.setBackgroundColor(Color.BLUE);

    return false;
}
于 2012-06-16T12:35:25.850 回答
1

我认为你应该使用

public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {


    Object obj = parent.getTag();
      if(obj instanceof View){
             ((View) obj).findViewByID(<id of main ViewGroup of row>).setBackgroundColor(Color.<your normal color>);
         }


    v.findViewByID(<id of main ViewGroup of row>).setBackgroundColor(Color.DKGRAY);


 parent.setTag(v);



    return false;
}


parent.getChildAt(childPosition).findViewByID(<id of main ViewGroup of row >).setBackgroundColor(Color.DKGRAY);

or 


v.findViewByID(<id of main ViewGroup of row>).setBackgroundColor(Color.DKGRAY);

第二个http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html#onChildClick(android.widget.ExpandableListView, android.view.View, int, int, long)

于 2012-06-16T12:25:06.817 回答