折叠或展开时,Android expandablelistview组/父级背景图像会发生变化

时间:2010-06-21 02:58:12

标签: android expandablelistview

我尝试在展开折叠或展开时交换expandablelistview组背景图片。但是背景图像没有正确交换。例如,当我展开第一组时,第二组或第三组的背景会被交换。

我正在使用RelativeLayout(组布局)和SimpleExpandableListAdapter(适配器)。这是我做的:

// Create 2 drawable background. One for collapse and one for expand
private Drawable collapseBG, expandBG;
private ExpandableListView myView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    collapseBG = getResources().getDrawable(R.drawable.box_bg_header_collapse);
    expandBG = getResources().getDrawable(R.drawable.box_bg_header_expand);
    myView = (ExpandableListView) findViewById(R.id.myView);
}

myView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
    public void onGroupCollapse(int groupPosition_c) {
        myView.getChildAt(groupPosition_c).setBackgroundDrawable(collapseBG);
    }
});

myView.setOnGroupExpandListener (new ExpandableListView.OnGroupExpandListener() {
    public void onGroupExpand(int groupPosition_e) {
        myView.getChildAt(groupPosition_e).setBackgroundDrawable(expandBG);
    }
});

有人知道如何使其有效吗?

2 个答案:

答案 0 :(得分:23)

我认为您应该制作自己的extends SimpleExpandableListAdapter适配器。在该课程中你应该覆盖

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
 View result = super.getGroupView(groupPosition, isExpanded, convertView, parent);

 if (isExpanded) {
     result.setBackgroundDrawable(expandBG);
 } else {
     result.setBackgroundDrawable(collapseBG);
 }

 return result;
}

在onCreate函数中你应该写如下的smth:

myView.setlistAdapter (new YourExtendedSimpleExpandableListAdapter( ...... ));

希望它会帮助你

答案 1 :(得分:1)

试试这个

//this is my list

ExpandableListView epView = (ExpandableListView) findViewById(R.id.TranscationList);

//set two listeners 

//i have to images  

epView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                 ImageView arrowDown;
                    ImageView arrowUp;
                    arrowUp = (ImageView)findViewById(R.id.arrowUp);
                    arrowDown =(ImageView)findViewById(R.id.arrowDown);
                      arrowUp.setVisibility(View.INVISIBLE);
                       arrowDown.setVisibility(View.VISIBLE);

            }
        });
epView.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int arg0) {
                  ImageView arrowDown;
                    ImageView arrowUp;
                    arrowUp = (ImageView)findViewById(R.id.arrowUp);
                    arrowDown =(ImageView)findViewById(R.id.arrowDown);
                      arrowUp.setVisibility(View.VISIBLE);
                       arrowDown.setVisibility(View.INVISIBLE);
            }
        });