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

use*_*801 3 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);
    }
});
Run Code Online (Sandbox Code Playgroud)

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

dav*_*avs 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;
}
Run Code Online (Sandbox Code Playgroud)

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

myView.setlistAdapter (new YourExtendedSimpleExpandableListAdapter( ...... ));
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助