Home | History | Annotate | Download | only in setupwizardlib
      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 
     17 package com.android.setupwizardlib;
     18 
     19 import android.annotation.TargetApi;
     20 import android.content.Context;
     21 import android.graphics.drawable.Drawable;
     22 import android.os.Build.VERSION_CODES;
     23 import android.support.v7.widget.RecyclerView;
     24 import android.support.v7.widget.RecyclerView.Adapter;
     25 import android.support.v7.widget.RecyclerView.ViewHolder;
     26 import android.util.AttributeSet;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 
     31 import com.android.setupwizardlib.template.RecyclerMixin;
     32 import com.android.setupwizardlib.template.RecyclerViewScrollHandlingDelegate;
     33 import com.android.setupwizardlib.template.RequireScrollMixin;
     34 
     35 /**
     36  * A GLIF themed layout with a RecyclerView. {@code android:entries} can also be used to specify an
     37  * {@link com.android.setupwizardlib.items.ItemHierarchy} to be used with this layout in XML.
     38  */
     39 public class GlifRecyclerLayout extends GlifLayout {
     40 
     41     protected RecyclerMixin mRecyclerMixin;
     42 
     43     public GlifRecyclerLayout(Context context) {
     44         this(context, 0, 0);
     45     }
     46 
     47     public GlifRecyclerLayout(Context context, int template) {
     48         this(context, template, 0);
     49     }
     50 
     51     public GlifRecyclerLayout(Context context, int template, int containerId) {
     52         super(context, template, containerId);
     53         init(context, null, 0);
     54     }
     55 
     56     public GlifRecyclerLayout(Context context, AttributeSet attrs) {
     57         super(context, attrs);
     58         init(context, attrs, 0);
     59     }
     60 
     61     @TargetApi(VERSION_CODES.HONEYCOMB)
     62     public GlifRecyclerLayout(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         mRecyclerMixin.parseAttributes(attrs, defStyleAttr);
     69         registerMixin(RecyclerMixin.class, mRecyclerMixin);
     70 
     71         final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class);
     72         requireScrollMixin.setScrollHandlingDelegate(
     73                 new RecyclerViewScrollHandlingDelegate(requireScrollMixin, getRecyclerView()));
     74     }
     75 
     76     @Override
     77     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
     78         super.onLayout(changed, left, top, right, bottom);
     79         mRecyclerMixin.onLayout();
     80     }
     81 
     82     @Override
     83     protected View onInflateTemplate(LayoutInflater inflater, int template) {
     84         if (template == 0) {
     85             template = R.layout.suw_glif_recycler_template;
     86         }
     87         return super.onInflateTemplate(inflater, template);
     88     }
     89 
     90     @Override
     91     protected void onTemplateInflated() {
     92         final View recyclerView = findViewById(R.id.suw_recycler_view);
     93         if (recyclerView instanceof RecyclerView) {
     94             mRecyclerMixin = new RecyclerMixin(this, (RecyclerView) recyclerView);
     95         } else {
     96             throw new IllegalStateException(
     97                     "GlifRecyclerLayout should use a template with recycler view");
     98         }
     99     }
    100 
    101     @Override
    102     protected ViewGroup findContainer(int containerId) {
    103         if (containerId == 0) {
    104             containerId = R.id.suw_recycler_view;
    105         }
    106         return super.findContainer(containerId);
    107     }
    108 
    109     @Override
    110     public View findManagedViewById(int id) {
    111         final View header = mRecyclerMixin.getHeader();
    112         if (header != null) {
    113             final View view = header.findViewById(id);
    114             if (view != null) {
    115                 return view;
    116             }
    117         }
    118         return super.findViewById(id);
    119     }
    120 
    121     /**
    122      * @see RecyclerMixin#setDividerItemDecoration(DividerItemDecoration)
    123      */
    124     public void setDividerItemDecoration(DividerItemDecoration decoration) {
    125         mRecyclerMixin.setDividerItemDecoration(decoration);
    126     }
    127 
    128     /**
    129      * @see RecyclerMixin#getRecyclerView()
    130      */
    131     public RecyclerView getRecyclerView() {
    132         return mRecyclerMixin.getRecyclerView();
    133     }
    134 
    135     /**
    136      * @see RecyclerMixin#setAdapter(Adapter)
    137      */
    138     public void setAdapter(Adapter<? extends ViewHolder> adapter) {
    139         mRecyclerMixin.setAdapter(adapter);
    140     }
    141 
    142     /**
    143      * @see RecyclerMixin#getAdapter()
    144      */
    145     public Adapter<? extends ViewHolder> getAdapter() {
    146         return mRecyclerMixin.getAdapter();
    147     }
    148 
    149     /**
    150      * @deprecated Use {@link #setDividerInsets(int, int)} instead.
    151      */
    152     @Deprecated
    153     public void setDividerInset(int inset) {
    154         mRecyclerMixin.setDividerInset(inset);
    155     }
    156 
    157     /**
    158      * @see RecyclerMixin#setDividerInset(int)
    159      */
    160     public void setDividerInsets(int start, int end) {
    161         mRecyclerMixin.setDividerInsets(start, end);
    162     }
    163 
    164     /**
    165      * @deprecated Use {@link #getDividerInsetStart()} instead.
    166      */
    167     @Deprecated
    168     public int getDividerInset() {
    169         return mRecyclerMixin.getDividerInset();
    170     }
    171 
    172     /**
    173      * @see RecyclerMixin#getDividerInsetStart()
    174      */
    175     public int getDividerInsetStart() {
    176         return mRecyclerMixin.getDividerInsetStart();
    177     }
    178 
    179     /**
    180      * @see RecyclerMixin#getDividerInsetEnd()
    181      */
    182     public int getDividerInsetEnd() {
    183         return mRecyclerMixin.getDividerInsetEnd();
    184     }
    185 
    186     /**
    187      * @see RecyclerMixin#getDivider()
    188      */
    189     public Drawable getDivider() {
    190         return mRecyclerMixin.getDivider();
    191     }
    192 }
    193