summaryrefslogtreecommitdiffstats
path: root/src/org/lineageos/eleven/widgets/SeparatedListAdapter.java
blob: 7af881aba1056544d26542e40ef7d564a80a3f6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

package org.lineageos.eleven.widgets;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

import org.lineageos.eleven.R;

import java.util.LinkedHashMap;
import java.util.Map;

public class SeparatedListAdapter extends BaseAdapter {

    public final Map<String, Adapter> mSections = new LinkedHashMap<String, Adapter>();

    public final ArrayAdapter<String> mHeaders;

    public final static int TYPE_SECTION_HEADER = 0;

    /**
     * Constructor of <code>SeparatedListAdapter</code>
     *
     * @param context The {@link Context} to use.
     */
    public SeparatedListAdapter(final Context context) {
        mHeaders = new ArrayAdapter<String>(context, R.layout.list_header);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getItem(int position) {
        for (final Object section : mSections.keySet()) {
            final Adapter adapter = mSections.get(section);
            final int size = adapter.getCount() + 1;

            // check if position inside this section
            if (position == 0) {
                return section;
            }
            if (position < size) {
                return adapter.getItem(position - 1);
            }

            // otherwise jump into next section
            position -= size;
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getCount() {
        // total together all mSections, plus one for each section header
        int total = 0;
        for (final Adapter adapter : mSections.values()) {
            total += adapter.getCount() + 1;
        }
        return total;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getViewTypeCount() {
        // assume that mHeaders count as one, then total all mSections
        int total = 1;
        for (final Adapter adapter : mSections.values()) {
            total += adapter.getViewTypeCount();
        }
        return total;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getItemViewType(int position) {
        int type = 1;
        for (final Object section : mSections.keySet()) {
            final Adapter adapter = mSections.get(section);
            final int size = adapter.getCount() + 1;

            // check if position inside this section
            if (position == 0) {
                return TYPE_SECTION_HEADER;
            }
            if (position < size) {
                return type + adapter.getItemViewType(position - 1);
            }

            // otherwise jump into next section
            position -= size;
            type += adapter.getViewTypeCount();
        }
        return -1;
    }

    public boolean areAllItemsSelectable() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isEnabled(final int position) {
        return getItemViewType(position) != TYPE_SECTION_HEADER;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public View getView(int position, final View convertView, final ViewGroup parent) {
        int sectionnum = 0;
        for (final Object section : mSections.keySet()) {
            final Adapter adapter = mSections.get(section);
            final int size = adapter.getCount() + 1;

            // check if position inside this section
            if (position == 0) {
                return mHeaders.getView(sectionnum, convertView, parent);
            }
            if (position < size) {
                return adapter.getView(position - 1, convertView, parent);
            }

            // otherwise jump into next section
            position -= size;
            sectionnum++;
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getItemId(final int position) {
        return position;
    }

    public void addSection(final String section, final Adapter adapter) {
        mHeaders.add(section);
        mSections.put(section, adapter);
    }

}