我希望有一个可展开的列表,显示两个不同的正常列表,一个包含来自/assets/
的静态数据,另一个包含从数据库中提取的数据。所示:
我不确定如何解决这个问题。如果我只是扩展BaseExpandableListAdapter
然后提供两个适配器作为组,将呼叫转移到notifyDataSetChanged
到ExpandableListAdapter
,我会没事吗?我还假设如果我想在子列表中显示页脚,我需要将它们添加为一行。这会有用吗?还有更好的选择吗?
答案 0 :(得分: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;
}
}