簡體   English   中英

在可擴展ListView Android中更改單擊子項的背景顏色

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

我想更改在ExpandableListView中單擊的子項的背景顏色。 也就是說,當點擊任何孩子時,它的背景顏色應該被改變。 我試圖以這種方式去它,但它選擇了一些其他行,而不是已被點擊的那一行。

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

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

    return false;
}

請告訴我可能缺少的東西。

這就是我解決它的方式。

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

怎么說讓你試着直接引用你的視圖並設置這樣的背景,

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

         v.setBackgroundColor(Color.BLUE);

    return false;
}

我想你應該用

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM