ListView:按部分对项目进行分组

时间:2014-03-26 18:01:37

标签: android listview navigation-drawer

以我的Navigation Drawer为例 Gmail应用,我想要按部分分组的ListView,类似于收件箱,所有标签

通过使用由"标头"分隔的多个ListView来实现此行为TextView(我必须手动构建),或者是AdapterListView支持的此部分分组行为?

2 个答案:

答案 0 :(得分:0)

我不确定Gmail应用是如何实现此行为的,但似乎您应该使用自定义适配器。使用多个列表视图不是解决此问题的有效方法,因为人们希望将数据行(消息)保存在单个列表项中。

答案 1 :(得分:0)

不要使用多个ListView,它会使滚动内容搞乱。

您所描述的内容只能使用一个ListView +适配器with multiple item view types,如下所示:

public class MyAdapter extends ArrayAdapter<Object> {

    // It's very important that the first item have a value of 0.
    // If not, the adapter won't work properly (I didn't figure out why yet)
    private int TYPE_SEPARATOR = 0;
    private int TYPE_DATA = 1;

    class Separator {
        String title;
    }

    public MyAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position).getClass().isAssignableFrom(Separator.class)) {
            return TYPE_SEPARATOR;
        }
        return TYPE_DATA;
    }

    @Override
    public int getViewTypeCount() {
        // Assuming you have only 2 view types
        return 2;
    }

    @Override
    public boolean isEnabled(int position) {
        // Mark separators as not enabled. That way, the onclick and onlongclik listener
        // won't be triggered for those items.
        return getItemViewType(position) != TYPE_SEPARATOR;
    }
}

您只需实现自己的getView方法即可正确呈现。

相关问题