SetupWizardPreferenceLayout.java revision 83862bb59558fc044de9aa0d6e9407be53af8b81
1/*
2 * Copyright (C) 2016 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.content.Context;
20import android.os.Bundle;
21import android.support.v7.widget.RecyclerView;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26
27import com.android.setupwizardlib.template.RecyclerMixin;
28
29/**
30 * A layout to be used with {@code PreferenceFragment} in v14 support library. This can be specified
31 * as the {@code android:layout} in the {@code app:preferenceFragmentStyle} in
32 * {@code app:preferenceTheme}.
33 *
34 * <p />Example:
35 * <pre>{@code
36 * &lt;style android:name="MyActivityTheme">
37 *     &lt;item android:name="preferenceTheme">@style/MyPreferenceTheme&lt;/item>
38 * &lt;/style>
39 *
40 * &lt;style android:name="MyPreferenceTheme">
41 *     &lt;item android:name="preferenceFragmentStyle">@style/MyPreferenceFragmentStyle&lt;/item>
42 * &lt;/style>
43 *
44 * &lt;style android:name="MyPreferenceFragmentStyle">
45 *     &lt;item android:name="android:layout">@layout/my_preference_layout&lt;/item>
46 * &lt;/style>
47 * }</pre>
48 *
49 * where {@code my_preference_layout} is a layout that contains
50 * {@link com.android.setupwizardlib.SetupWizardPreferenceLayout}.
51 *
52 * <p />Example:
53 * <pre>{@code
54 * &lt;com.android.setupwizardlib.SetupWizardPreferenceLayout
55 *     xmlns:android="http://schemas.android.com/apk/res/android"
56 *     android:id="@id/list_container"
57 *     android:layout_width="match_parent"
58 *     android:layout_height="match_parent" />
59 * }</pre>
60 *
61 * <p />Fragments using this layout <em>must</em> delegate {@code onCreateRecyclerView} to the
62 * implementation in this class: {@link #onCreateRecyclerView}
63 */
64public class SetupWizardPreferenceLayout extends SetupWizardRecyclerLayout {
65
66    public SetupWizardPreferenceLayout(Context context) {
67        super(context);
68    }
69
70    public SetupWizardPreferenceLayout(Context context, int template, int containerId) {
71        super(context, template, containerId);
72    }
73
74    public SetupWizardPreferenceLayout(Context context, AttributeSet attrs) {
75        super(context, attrs);
76    }
77
78    public SetupWizardPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
79        super(context, attrs, defStyleAttr);
80    }
81
82    @Override
83    protected ViewGroup findContainer(int containerId) {
84        if (containerId == 0) {
85            containerId = R.id.suw_layout_content;
86        }
87        return super.findContainer(containerId);
88    }
89
90    /**
91     * This method must be called in {@code PreferenceFragment#onCreateRecyclerView}.
92     */
93    public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
94            Bundle savedInstanceState) {
95        return mRecyclerMixin.getRecyclerView();
96    }
97
98    @Override
99    protected View onInflateTemplate(LayoutInflater inflater, int template) {
100        if (template == 0) {
101            template = R.layout.suw_preference_template;
102        }
103        return super.onInflateTemplate(inflater, template);
104    }
105
106    @Override
107    protected void onTemplateInflated() {
108        // Inflate the recycler view here, so attributes on the decoration views can be applied
109        // immediately.
110        final LayoutInflater inflater = LayoutInflater.from(getContext());
111        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
112                R.layout.suw_preference_recycler_view, this, false);
113        mRecyclerMixin = new RecyclerMixin(this, recyclerView);
114    }
115}
116