开发者

Android expandablelistview group/parent background image change when collapse or expand

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.

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.On开发者_Go百科GroupExpandListener() {
    public void onGroupExpand(int groupPosition_e) {
        myView.getChildAt(groupPosition_e).setBackgroundDrawable(expandBG);
    }
});

Is anyone know how to make this works?


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

in onCreate function you should write smth like following:

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

hope it will help you


try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜