0

我有一个包含 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);
  }
}
4

1 回答 1

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 
}
于 2013-01-07T14:22:24.150 回答