【问题标题】:Android - Better alternative to expandable listview?Android - 可扩展列表视图的更好替代方案?
【发布时间】:2012-10-19 10:31:29
【问题描述】:

我有一个从 Web 服务 JSON 加载的项目列表。这些项目中的每一个都有自己的子项目列表。每个项目都有许多文本视图。子项的数量动态变化,并且由多个文本视图组成。有时有几十个项目可能有数百个子项目。我希望所有信息始终可见,即我实际上不需要可扩展列表视图的 "expandable" 部分,但它似乎是获取子项和组布局的最简单方法。

Item1
  sub item 1
  sub item 2

Item 2
  sub item 1
  sub item 2
  sub item 3

etc

我目前已经实现了一个可扩展的列表视图并强制组总是被扩展。这行得通,但是滚动很慢,不仅在模拟器上,而且在我的 Galaxy s2 上。
我想知道是否有更好的方法来实现这一点?
我考虑了单个列表视图并将子项中的字符串连接到单个文本视图中,但认为这在美学上并不令人愉悦,并且我将无法获得我想要的布局。
我想知道是否有一种方法可以覆盖已调整的列表视图,以便其中一个项目本身就是其他项目的数组。适配器可以迭代该数组并动态创建新的文本视图。我想这就是可扩展列表视图正在做的事情,所以也许这最终会导致同样多的滞后滚动。

非常感谢任何帮助

【问题讨论】:

  • 如何创建一个普通的列表视图,然后为标题添加一种视图,为子级添加另一种视图?这并不完全是一个答案,但会适合您的情况。
  • 您可以在内部列表视图的宫殿中使用微调器。
  • 如果您有想要显示的项目列表,最好和最有效的方法是使用RecyclerView 并使用 DiffUtil 更新它。这是关于RecyclerView的官方文档:developer.android.com/guide/topics/ui/layout/recyclerview

标签: android json listview expandablelistview


【解决方案1】:

您可以使用 ListView 和自定义适配器类来实现。

请注意,适配器有 int getViewTypeCount()int getItemViewType(int position) 方法,您可以覆盖它们。

查看列表地图中数据集的示例实现

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

及用法示例:

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's 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);
    }
}

【讨论】:

  • 好主意,所以我也使用了这个示例 (bartinger.at/listview-with-sectionsseparators),因为我无法完全遵循您的代码。我现在有了我想要的东西,除了一些奇怪的原因我的物品出现故障?!正在解析的 JSON 具有按日期降序排列的项目,但是当它们出现在列表视图中时,它们是随机的,日期随处可见。正确的孩子链接到正确的父母。有什么想法吗?
  • 好的,找到原因了,这是因为我解析的是对象而不是数组,所以没有维护顺序。但是由于我的 JSON 的限制,我无法解析数组。完全不相关的问题,所以我将继续研究如何解决这个问题,以便我可以按顺序遍历 json 对象
  • @oli107 - 你刚刚让我意识到,在上面的示例中,应该使用 LinkedHashMap 或 TreeMap,因为其他地图实现可能无法保持键的顺序(已编辑)。至于 json 对象 - 它们不需要保持键的原始顺序(与维护此顺序的 javascript 相反)。所以为了保持顺序,你不应该使用 android sdk 中的JSONObject,而是手动解析 json 字符串或寻找一些保证保持原始键顺序的 json 库。 (我建议 jackson jackson.codehaus.org 并查看 @JsonPropertyOrder 的文档)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
相关资源
最近更新 更多