滚动时更改可扩展列表的子项中的背景颜色

时间:2013-01-04 12:53:08

标签: android scroll expandablelistview

我有一个包含3个项目的ExpandableList,每个项目都有不同的项目,具有不同的属性,例如背景颜色,图像,......

我想点击它时更改子项的颜色。我不滚动时可以改变颜色,因为我用视图保存了标签。但是当我滚动并点击其他孩子时会出现问题。选择的最后一个孩子不会更改颜色背景。

expandableList.setOnClickListener(new OnChildClickListener(){
...
  View view =(View)parent.getTag();
  //Change last view colour
  view.setBackgroundResourece(R.drawable.background_noselect);
  ...
  parent.setTag(v);
}

在适配器中:

getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
  ...
  if(isSelect()){
      convertView.setBackgroundResource(R.drawable.background_select);
  }
}

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,当我滚动expandableList时,我无法更改视图的颜色。我做了两件事。首先是我在活动中添加了这段代码:

expandableList.setOnChildClickListener(new OnChildClickListener(){
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            View view= null ;   
                //Select's item     
            select  = expandableListInfo[groupPosition].get(childPosition);
                            //Get the last element visible in expandableList
            int lastVis =expandableList.getLastVisiblePosition();
                            //Get the first element vsible in the expandableList
            int firstVis = listadoPedidos.getFirstVisiblePosition();
            int count = firstVis;
            while (count <= lastVis) {
                int viewPosition=count-firstVis;
                long longposition = expandableList.getExpandableListPosition(count);
                int type = ExpandableListView.getPackedPositionType(longposition);
                if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                   int groupPositionLast = ExpandableListView.getPackedPositionGroup(longposition); 
                   //Get the object selected before
                   //This tag is save in the adapter               
                   Object lastObject = (Object)parent.getTag(R.id.tag_select);
                   view =(View) parent.getChildAt(viewPosition);
                   Object ob=view.getTag(R.id.tag_select);
                   if(ob.equals(lastObject)){
                       //Change colour view
                   }
                }
            count++;
            }
           ...
            parent.setTag(R.id.tag_select, select);

            return false;
        }

    });

第二件事是在适配器中添加getChildView下一个代码:

convertView.setTag(R.id.tag_select, object);
if(parent.getTag(R.id.tag_select).equals.object){ 
 //// TODO 
}