安卓expandablelistview组/母公司的背景图像变化时,折叠或展开母公司、图像、背景、expandablelistview

2023-09-05 09:38:04 作者:吃醋小王子i

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

I try to swap expandablelistview group background image when it is collapse or expand. But the background image was not correctly swap. For example when I expand the first group, background of second or third group get swapped.

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

I am using RelativeLayout (group layout) and SimpleExpandableListAdapter (adapter). Here is what I did:

// 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);
    }
});

有没有人知道如何使这项工作?

Is anyone know how to make this works?

推荐答案

我想你应该让自己的适配器,它扩展SimpleExpandableListAdapter 。在这个类,你应该重写

I think you should to make your own adapter which extends SimpleExpandableListAdapter. In that class you should override

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功能,你应该写水木清华类似以下内容:

in onCreate function you should write smth like following:

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

希望它会帮助你。

hope it will help you