Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android expandablelistview组/父背景图像在折叠或展开时发生更改_Android_Expandablelistview - Fatal编程技术网

Android expandablelistview组/父背景图像在折叠或展开时发生更改

Android expandablelistview组/父背景图像在折叠或展开时发生更改,android,expandablelistview,Android,Expandablelistview,我尝试在折叠或展开时交换expandablelistview组背景图像。但是背景图像没有正确交换。例如,当我展开第一组时,第二组或第三组的背景被交换 我使用的是RelativeLayout(组布局)和SimpleExpandableListAdapter(适配器)。以下是我所做的: // Create 2 drawable background. One for collapse and one for expand private Drawable collapseBG, expandBG;

我尝试在折叠或展开时交换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);
    }
});

有人知道如何使其工作吗?

我认为您应该制作自己的适配器,它
扩展了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( ...... ));
希望它能帮助你试试这个

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

谢谢注意:要覆盖的方法名是getGroupView,而不是getViewGroup(不存在)。谢谢davs。。你救了我一天……)