Best alternative to extensible list?

I have a list of items downloaded from a JSON web service. Each of these elements has its own list of subitems. Each element has many textual representations. The number of sub-elements changes dynamically and consists of several text views. There are at times dozens of items with potentially hundreds of sub-elements. I want all information to be displayed constantly, i.e. I really don't need the extensible part of the extensible list, but it seemed like the easiest way to get children and group layout.

Item1 sub item 1 sub item 2 Item 2 sub item 1 sub item 2 sub item 3 etc 

I am currently implementing an extensible listview and forcing groups to always expand. This works, however, it scrolls very slowly, not only on the emulator, but also on my galaxy s2.
I am wondering if there is a better way to achieve this?
I looked at one view of the list and combined the lines in the subelements into one text view, but I think that it will not be aesthetically pleasing, and I will not be able to get the layout I want. I wondered if there is a way to override the rewritten list so that one of the elements is an array of other elements. An adapter can iterate over this array and create new text messages on the fly. I guess this is what extensible list browsing does, so maybe it will end up with the same idle scrolling.

Any help is much appreciated

+7
source share
1 answer

You can achieve this with the ListView class and custom adapter.

Note that the adapter has int getViewTypeCount() and int getItemViewType(int position) methods that you can overwrite.

Look at a sample implementation of a dataset in Map of lists

 public abstract class ExampleMapAdapter<V extends Map<?, ? extends List<?>>> extends BaseAdapter { public static final int VIEW_TYPE_HEADER = 0; public static final int VIEW_TYPE_LISTITEM = 1; protected V data; protected int[] sectionsStart; protected Object[] sections; protected int count; public ExampleMapAdapter(V data) { this.data = data; onSetData(); } @Override public int getCount() { return count; } @Override public Object getItem(int position) { int sectionIndex = getSectionForPosition(position); int innerIndex = position - sectionsStart[sectionIndex]; if(innerIndex == 0) { //head return sections[sectionIndex]; } else { //values return data.get(sections[sectionIndex]).get(innerIndex - 1); } } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { return Arrays.binarySearch(sectionsStart, position) < 0 ? VIEW_TYPE_LISTITEM : VIEW_TYPE_HEADER; } public int getPositionForSection(int section) { return sectionsStart[section]; } public int getSectionForPosition(int position) { int section = Arrays.binarySearch(sectionsStart, position); return section < 0 ? -section - 2 : section; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(getItemViewType(position) == VIEW_TYPE_HEADER) { return getHeaderView(position, convertView, parent); } else { return getListItemView(position, convertView, parent); } } @Override public void notifyDataSetInvalidated() { data = null; onSetData(); super.notifyDataSetInvalidated(); } @Override public void notifyDataSetChanged() { onSetData(); super.notifyDataSetChanged(); } protected void onSetData() { if(data == null) { sectionsStart = null; sections = null; count = 0; } else { sectionsStart = new int[data.size()]; sections = data.keySet().toArray(new Object[data.size()]); count = 0; int i = 0; for(List<?> v : data.values()) { sectionsStart[i] = count; i++; count += 1 + v.size(); } } } protected abstract View getHeaderView(int position, View convertView, ViewGroup parent); protected abstract View getListItemView(int position, View convertView, ViewGroup parent); } 

and usage example:

 public class ExampleActivity extends Activity { class SubItem { public final String text; public final int number; public final boolean checked; public SubItem(String text, int number, boolean checked) { this.text = text; this.number = number; this.checked = checked; } } //use LinkedHashMap or TreeMap as other map implementations may not keep key order final Map<String, List<SubItem>> map = new LinkedHashMap<String, List<SubItem>>(); { List<SubItem> list = new ArrayList<SubItem>(); list.add(new SubItem("sub item", 1, false)); list.add(new SubItem("sub item", 2, true)); map.put("Item1", list); list = new ArrayList<SubItem>(); list.add(new SubItem("sub item", 1, true)); list.add(new SubItem("sub item", 2, false)); list.add(new SubItem("sub item", 3, false)); map.put("Item2", list); } @Override protected void onCreate(Bundle savedInstanceState) { ListView vList = new ListView(this); vList.setAdapter(new ExampleMapAdapter<Map<String, List<SubItem>>>(map) { @Override protected View getHeaderView(int position, View convertView, ViewGroup parent) { TextView v = convertView == null ? new TextView(parent.getContext()) : (TextView) convertView; v.setText((String) getItem(position)); return v; } @Override protected View getListItemView(int position, View convertView, ViewGroup parent) { CheckBox v = convertView == null ? new CheckBox(parent.getContext()) : (CheckBox) convertView; SubItem item = (SubItem) getItem(position); v.setText(item.text + " " + item.number); v.setChecked(item.checked); return v; } }); setContentView(vList); } } 
+2
source

All Articles