Change Background Color of Clicked Child in Expandable ListView Android

前端 未结 3 786
暗喜
暗喜 2021-01-19 03:46

I want to change the background color of the child which is clicked in an ExpandableListView. That is, when any child is clicked, it\'s background color should get changed.

相关标签:
3条回答
  • 2021-01-19 04:08

    This is how I solved it.

    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;
    }
    
    0 讨论(0)
  • 2021-01-19 04:21

    I think you should use

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

    for second one http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html#onChildClick(android.widget.ExpandableListView, android.view.View, int, int, long)

    0 讨论(0)
  • 2021-01-19 04:30

    How about lets say you try to directly refer your view and set the background like this,

    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
    
             v.setBackgroundColor(Color.BLUE);
    
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题