安卓expandablelistview组/母公司的背景图像变化时,折叠或展开 [英] Android expandablelistview group/parent background image change when collapse or expand

查看:116
本文介绍了安卓expandablelistview组/母公司的背景图像变化时,折叠或展开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着换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

这篇关于安卓expandablelistview组/母公司的背景图像变化时,折叠或展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆