GlifListLayout.java revision e15b8a2489610e3a6fe0a5bc2e26625b067631e0
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.setupwizardlib;
18
19import android.annotation.TargetApi;
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.os.Build.VERSION_CODES;
23import android.util.AttributeSet;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ListAdapter;
28import android.widget.ListView;
29
30import com.android.setupwizardlib.items.ItemAdapter;
31import com.android.setupwizardlib.items.ItemGroup;
32import com.android.setupwizardlib.items.ItemInflater;
33
34/**
35 * A GLIF themed layout with a ListView. {@code android:entries} can also be used to specify an
36 * {@link com.android.setupwizardlib.items.ItemHierarchy} to be used with this layout in XML.
37 */
38public class GlifListLayout extends GlifLayout {
39
40    private static final String TAG = "GlifListLayout";
41    private ListView mListView;
42
43    public GlifListLayout(Context context) {
44        this(context, 0, 0);
45    }
46
47    public GlifListLayout(Context context, int template) {
48        this(context, template, 0);
49    }
50
51    public GlifListLayout(Context context, int template, int containerId) {
52        super(context, template, containerId);
53        init(context, null, 0);
54    }
55
56    public GlifListLayout(Context context, AttributeSet attrs) {
57        super(context, attrs);
58        init(context, attrs, 0);
59    }
60
61    @TargetApi(VERSION_CODES.HONEYCOMB)
62    public GlifListLayout(Context context, AttributeSet attrs, int defStyleAttr) {
63        super(context, attrs, defStyleAttr);
64        init(context, attrs, defStyleAttr);
65    }
66
67    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
68        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwGlifListLayout,
69                defStyleAttr, 0);
70        final int xml = a.getResourceId(R.styleable.SuwGlifListLayout_android_entries, 0);
71        if (xml != 0) {
72            final ItemGroup inflated = (ItemGroup) new ItemInflater(context).inflate(xml);
73            setAdapter(new ItemAdapter(inflated));
74        }
75        a.recycle();
76    }
77
78    @Override
79    protected View onInflateTemplate(LayoutInflater inflater, int template) {
80        if (template == 0) {
81            template = R.layout.suw_glif_list_template;
82        }
83        return super.onInflateTemplate(inflater, template);
84    }
85
86    @Override
87    protected ViewGroup findContainer(int containerId) {
88        if (containerId == 0) {
89            containerId = android.R.id.list;
90        }
91        return super.findContainer(containerId);
92    }
93
94    @Override
95    protected void onTemplateInflated() {
96        mListView = (ListView) findViewById(android.R.id.list);
97    }
98
99    public ListView getListView() {
100        return mListView;
101    }
102
103    public void setAdapter(ListAdapter adapter) {
104        getListView().setAdapter(adapter);
105    }
106
107    public ListAdapter getAdapter() {
108        return getListView().getAdapter();
109    }
110}
111