2010-06-21 24 views
3

當崩潰或展開時,我嘗試將expandablelistview組背景圖像互換。但背景圖片沒有正確交換。例如,當我展開第一組時,第二組或第三組的背景會被交換。崩潰或展開時Android展開式列表視圖組/父級背景圖像更改

我使用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); 
    } 
}); 

有人知道如何使這個作品?

回答

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

中的onCreate功能,你應該寫像水木清華以下:

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

希望它會幫助你

+0

感謝。一種觀察:重寫的方法名稱是getGroupView而不是getViewGroup(不存在)。 – 2010-12-08 18:16:33

+0

是的,你是對的。謝謝。我做了更改 – davs 2010-12-08 20:43:18

+0

謝謝davs .. U救了我的一天.. :) – Jomia 2015-02-25 13:24:42

1

試試這個

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