2013-12-17 55 views
1

我想要一個可擴展列表顯示兩個不同的正常列表,其中一個使用來自/assets/的靜態數據,另一個使用從數據庫獲取的數據。圖說:ExpandableListAdapter包含兩個不同的適配器,以提供組

enter image description here

我真的不知道如何去了解這一點。如果我只是擴展BaseExpandableListAdapter,然後提供兩個適配器作爲組,將電話轉到notifyDataSetChangedExpandableListAdapter,我可以嗎?我還假設如果我想在子列表中顯示頁腳,我需要將它們添加爲一行。這會工作嗎?有更好的選擇嗎?

回答

2

當然!以此課程爲出發點:

請注意,子視圖是可回收的,無論它們來自哪個組。

public static class MergeAdapter extends BaseExpandableListAdapter { 
    private final List<Adapter> adapters = new ArrayList<Adapter>(2); 
    private final Context ctx; 

    public MergeAdapter(Context ctx) { 
     List<String> firstList = new ArrayList<String>(3); 
     firstList.add("one"); 
     firstList.add("two"); 
     firstList.add("three"); 
     List<String> secondList = new ArrayList<String>(3); 
     secondList.add("fo'"); 
     secondList.add("five"); 
     secondList.add("six"); 

     adapters.add(new ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, firstList)); 
     adapters.add(new ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, secondList)); 
     this.ctx = ctx; 
    } 

    @Override 
    public int getGroupCount() { 
     return adapters.size(); 
    } 

    @Override 
    public int getChildrenCount(int i) { 
     return adapters.get(i).getCount(); 
    } 

    @Override 
    public Adapter getGroup(int i) { 
     return adapters.get(i); 
    } 

    @Override 
    public Object getChild(int i, int i2) { 
     return adapters.get(i).getItem(i2); 
    } 

    @Override 
    public long getGroupId(int i) { 
     return i; 
    } 

    @Override 
    public long getChildId(int i, int i2) { 
     return adapters.get(i).getItemId(i2); 
    } 

    @Override 
    public boolean hasStableIds() { 
     //In our case true, but for dynamic data likely to be false 
     return false; 
    } 

    @Override 
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) { 
     if (view == null) { 
      LayoutInflater inflater = LayoutInflater.from(ctx); 
      //Better to use layout custom-made for expandable lists... 
      view = inflater.inflate(android.R.layout.simple_list_item_1, viewGroup, false); 
     } 
     TextView tv = (TextView) view.findViewById(android.R.id.text1); 
     tv.setText("List " + i); 
     return view; 
    } 

    @Override 
    public View getChildView(int i, int i2, boolean isLastView, View view, ViewGroup viewGroup) { 
     //isLastView will be handy if you want to make a footer 
     return adapters.get(i).getView(i2, view, viewGroup); 
    } 

    @Override 
    public boolean isChildSelectable(int i, int i2) { 
     //Customize 
     return true; 
    } 
} 
相關問題