2012-04-10 73 views
0

我已經創建了一個自定義Expandable列表,當我點擊它,將移動到子項目, 我可以使用點擊監聽器控制可擴展列表(例如,如果我點擊組項目之一,它應該去其他視圖(佈局),而不是子項 去這是我的代碼通過添加onclick監聽器事件來控制ExpandableList。

public class MainActivity extends Activity 
    { 

     private ExpandListAdapter ExpAdapter; 
     private ArrayList<ExpandListGroup> ExpListItems; 
     private ExpandableListView ExpandList; 
     Header1 header; 
     ExpandableList ExpandableList; 

     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.main); 
      header = (Header1)findViewById(R.id.header); 
      header.initHeader(); 
      ExpandableList = (ExpandableList)findViewById(R.id.Expanadable); 
      ExpandableList.Expandableview(); 
      ExpandList = (ExpandableListView) findViewById(R.id.ExpList); 
      ExpListItems = SetStandardGroups(); 
      ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems); 
      ExpandList.setAdapter(ExpAdapter); 
     } 

     public ArrayList<ExpandListGroup> SetStandardGroups() { 
      ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>(); 
      ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>(); 
      ArrayList<ExpandListChild> list3 = new ArrayList<ExpandListChild>(); 

      ExpandListGroup gru1 = new ExpandListGroup(); 
      gru1.setName("Employee Search"); 

      ExpandListChild ch1_1 = new ExpandListChild(); 
      ch1_1.setName("First Name"); 
      ch1_1.setTag(null); 
      list2.add(ch1_1); 

      ExpandListChild ch1_2 = new ExpandListChild(); 
      ch1_2.setName("Last Name"); 
      ch1_2.setTag(null); 
      list2.add(ch1_2); 

      ExpandListChild ch1_3 = new ExpandListChild(); 
      ch1_3.setName("Email ID"); 
      ch1_3.setTag(null); 
      list2.add(ch1_3); 

      ExpandListChild ch1_4 = new ExpandListChild(); 
      ch1_4.setName("Associate ID"); 
      ch1_4.setTag(null); 
      list2.add(ch1_4); 
      gru1.setItems(list2); 
      list2 = new ArrayList<ExpandListChild>(); 

      ExpandListGroup gru2 = new ExpandListGroup(); 
      gru2.setName("Working Time"); 

      ExpandListChild ch2_1 = new ExpandListChild(); 
      ch2_1.setName("Leave Request"); 
      ch2_1.setTag(null); 
      list2.add(ch2_1); 

      ExpandListGroup gru4 = new ExpandListGroup(); 
      gru4.setName("Leave Request"); 
      list3 = new ArrayList<ExpandListChild>(); 
      ExpandListChild ch21_1 = new ExpandListChild(); 
      ch21_1.setName("create Leave"); 
      ch21_1.setTag(null); 
      list3.add(ch21_1); 
      gru4.setItems(list3); 

      ExpandListChild ch2_2 = new ExpandListChild(); 
      ch2_2.setName("CATS Regular/Record Working time"); 
      ch2_2.setTag(null); 
      list2.add(ch2_2); 

      /* ExpandListChild ch2_3 = new ExpandListChild(); 
      ch2_3.setName("And an other movie"); 
      ch2_3.setTag(null); 
      list2.add(ch2_3);*/ 
      gru2.setItems(list2); 

      list2 = new ArrayList<ExpandListChild>(); 
      ExpandListGroup gru3 = new ExpandListGroup(); 
      gru3.setName("Travel and Expenses"); 

      ExpandListChild ch3_1 = new ExpandListChild(); 
      ch3_1.setName("Travel Request"); 
      ch3_1.setTag(null); 
      list2.add(ch3_1); 

      ExpandListChild ch3_2 = new ExpandListChild(); 
      ch3_2.setName("Travel Expenses"); 
      ch3_2.setTag(null); 
      list2.add(ch3_2); 
      gru3.setItems(list2); 

      list.add(gru1); 
      list.add(gru2); 
      list.add(gru3); 

      return list; 
     } 

    } 

現在,如果我點擊「employeesearch」組應該移動到另一個佈局,並保持剩餘組作爲相同擴展列表。下面是適配器類

public class ExpandListAdapter extends BaseExpandableListAdapter { 

    private Context context; 
    private ArrayList<ExpandListGroup> groups; 
    public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) { 
     this.context = context; 
     this.groups = groups; 
    } 

    public void addItem(ExpandListChild item, ExpandListGroup group) { 
     if (!groups.contains(group)) { 
      groups.add(group); 
     } 
     int index = groups.indexOf(group); 
     ArrayList<ExpandListChild> ch = groups.get(index).getItems(); 
     ch.add(item); 
     groups.get(index).setItems(ch); 
    } 
    public Object getChild(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems(); 
     return chList.get(childPosition); 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return childPosition; 
    } 

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, 
      ViewGroup parent) { 
     ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition); 
     if (view == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      view = infalInflater.inflate(R.layout.expandlist_child_item, null); 
     } 
     TextView tv = (TextView) view.findViewById(R.id.tvChild); 
     tv.setText(child.getName().toString()); 
     tv.setTag(child.getTag()); 
     // TODO Auto-generated method stub 
     return view; 
    } 

    public int getChildrenCount(int groupPosition) { 
     // TODO Auto-generated method stub 
     ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems(); 

     return chList.size(); 

    } 

    public Object getGroup(int groupPosition) { 
     // TODO Auto-generated method stub 
     return groups.get(groupPosition); 
    } 

    public int getGroupCount() { 
     // TODO Auto-generated method stub 
     return groups.size(); 
    } 

    public long getGroupId(int groupPosition) { 
     // TODO Auto-generated method stub 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isLastChild, View view, 
      ViewGroup parent) { 
     ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition); 
     if (view == null) { 
      LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      view = inf.inflate(R.layout.expandlist_group_item, null); 
     } 
     TextView tv = (TextView) view.findViewById(R.id.tvGroup); 
     tv.setText(group.getName()); 
     // TODO Auto-generated method stub 
     return view; 
    } 

    public boolean hasStableIds() { 
     // TODO Auto-generated method stub 
     return true; 
    } 

    public boolean isChildSelectable(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return true; 
    } 

} 

回答

0

似乎很好用f或onGroupClick

從文檔:

public abstract boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 
Since: API Level 1 
Callback method to be invoked when a group in this expandable list has been clicked. 
Parameters 
parent   The ExpandableListConnector where the click happened 
v     The view within the expandable list/ListView that was clicked 
groupPosition  The group position that was clicked 
id    The row id of the group that was clicked 
+0

謝謝。非常 – 2012-04-10 13:19:17