3

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

我正在使用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);
    }
});

有谁知道如何使这个工作?

4

2 回答 2

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 函数中,您应该编写如下内容:

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

希望对你有帮助

于 2010-06-21T11:53:35.500 回答
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);
            }
        });
于 2011-08-11T06:47:33.917 回答