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