简体   繁体   English

在可扩展ListView Android中更改单击子项的背景颜色

[英]Change Background Color of Clicked Child in Expandable ListView Android

I want to change the background color of the child which is clicked in an ExpandableListView. 我想更改在ExpandableListView中单击的子项的背景颜色。 That is, when any child is clicked, it's background color should get changed. 也就是说,当点击任何孩子时,它的背景颜色应该被改变。 I am trying to go it in this way but it selects some other row, not the one which has been clicked. 我试图以这种方式去它,但它选择了一些其他行,而不是已被点击的那一行。

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

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

    return false;
}

Please tell me what I might be missing. 请告诉我可能缺少的东西。

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

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

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) 第二个http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html#onChildClick(android.widget.ExpandableListView,android.view.View,int,int,long)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在onChildClick()方法之外更改Expandable ListView Android中被单击子项的背景颜色 - Change Background Color of Clicked Child in Expandable ListView Android outside the onChildClick() method 当在扩展列表视图中单击“子项”中的按钮时,Android更改父项的背景 - Android Change Background of Parent when Button in Child is clicked in Expandable ListView Android-如何更改可扩展listView中单个孩子的背景颜色? - Android - how can change background color of a single child in expandable listView? 如何仅更改 Android Studio 中 ListView 中单击项的背景颜色? - How to change background color of only the clicked item in ListView in Android Studio? 如何在Android中的可扩展列表视图中单击后禁用子项 - How to disable a child item once clicked in an expandable listview in Android 我可以在android的可扩展列表视图中更改子行的背景颜色吗 - Can I change the background color of child row on expandable list view in android 单击时更改列表视图项的背景颜色 - change the background color of a listview item when clicked Android ListView背景颜色更改 - Android ListView background color change 在ExpandableListView中更改被单击子项的背景颜色 - Change Background Color of Clicked Child in ExpandableListView Android Expandable ListView子级点击 - Android Expandable ListView Child Click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM