GlifListLayout.java revision 0ceb8d53e39ebb5bc103863787afb39ec5c41ad8
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.graphics.drawable.Drawable;
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.template.ListMixin;
31import com.android.setupwizardlib.template.ListViewScrollHandlingDelegate;
32import com.android.setupwizardlib.template.RequireScrollMixin;
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    /* static section */
41
42    private static final String TAG = "GlifListLayout";
43
44    /* non-static section */
45
46    private ListMixin mListMixin;
47
48    public GlifListLayout(Context context) {
49        this(context, 0, 0);
50    }
51
52    public GlifListLayout(Context context, int template) {
53        this(context, template, 0);
54    }
55
56    public GlifListLayout(Context context, int template, int containerId) {
57        super(context, template, containerId);
58        init(context, null, 0);
59    }
60
61    public GlifListLayout(Context context, AttributeSet attrs) {
62        super(context, attrs);
63        init(context, attrs, 0);
64    }
65
66    @TargetApi(VERSION_CODES.HONEYCOMB)
67    public GlifListLayout(Context context, AttributeSet attrs, int defStyleAttr) {
68        super(context, attrs, defStyleAttr);
69        init(context, attrs, defStyleAttr);
70    }
71
72    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
73        mListMixin = new ListMixin(this, attrs, defStyleAttr);
74        registerMixin(ListMixin.class, mListMixin);
75
76        final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class);
77        requireScrollMixin.setScrollHandlingDelegate(
78                new ListViewScrollHandlingDelegate(requireScrollMixin, getListView()));
79    }
80
81    @Override
82    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
83        super.onLayout(changed, left, top, right, bottom);
84        mListMixin.onLayout();
85    }
86
87    @Override
88    protected View onInflateTemplate(LayoutInflater inflater, int template) {
89        if (template == 0) {
90            template = R.layout.suw_glif_list_template;
91        }
92        return super.onInflateTemplate(inflater, template);
93    }
94
95    @Override
96    protected ViewGroup findContainer(int containerId) {
97        if (containerId == 0) {
98            containerId = android.R.id.list;
99        }
100        return super.findContainer(containerId);
101    }
102
103    public ListView getListView() {
104        return mListMixin.getListView();
105    }
106
107    public void setAdapter(ListAdapter adapter) {
108        mListMixin.setAdapter(adapter);
109    }
110
111    public ListAdapter getAdapter() {
112        return mListMixin.getAdapter();
113    }
114
115    /**
116     * Sets the start inset of the divider. This will use the default divider drawable set in the
117     * theme and inset it {@code inset} pixels to the right (or left in RTL layouts).
118     *
119     * @param inset The number of pixels to inset on the "start" side of the list divider. Typically
120     *              this will be either {@code @dimen/suw_items_glif_icon_divider_inset} or
121     *              {@code @dimen/suw_items_glif_text_divider_inset}.
122     *
123     * @see ListMixin#setDividerInset(int)
124     */
125    public void setDividerInset(int inset) {
126        mListMixin.setDividerInset(inset);
127    }
128
129    /**
130     * @see ListMixin#getDividerInset()
131     */
132    public int getDividerInset() {
133        return mListMixin.getDividerInset();
134    }
135
136    /**
137     * @see ListMixin#getDivider()
138     */
139    public Drawable getDivider() {
140        return mListMixin.getDivider();
141    }
142}
143