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 GlifRecyclerLayout(Context context)43 public GlifRecyclerLayout(Context context) { 44 this(context, 0, 0); 45 } 46 GlifRecyclerLayout(Context context, int template)47 public GlifRecyclerLayout(Context context, int template) { 48 this(context, template, 0); 49 } 50 GlifRecyclerLayout(Context context, int template, int containerId)51 public GlifRecyclerLayout(Context context, int template, int containerId) { 52 super(context, template, containerId); 53 init(context, null, 0); 54 } 55 GlifRecyclerLayout(Context context, AttributeSet attrs)56 public GlifRecyclerLayout(Context context, AttributeSet attrs) { 57 super(context, attrs); 58 init(context, attrs, 0); 59 } 60 61 @TargetApi(VERSION_CODES.HONEYCOMB) GlifRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr)62 public GlifRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr) { 63 super(context, attrs, defStyleAttr); 64 init(context, attrs, defStyleAttr); 65 } 66 init(Context context, AttributeSet attrs, int defStyleAttr)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 onLayout(boolean changed, int left, int top, int right, int bottom)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 onInflateTemplate(LayoutInflater inflater, int template)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 onTemplateInflated()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 findContainer(int containerId)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 // Returning generic type is the common pattern used for findViewBy* methods 111 @SuppressWarnings("TypeParameterUnusedInFormals") findManagedViewById(int id)112 public <T extends View> T findManagedViewById(int id) { 113 final View header = mRecyclerMixin.getHeader(); 114 if (header != null) { 115 final T view = header.findViewById(id); 116 if (view != null) { 117 return view; 118 } 119 } 120 return super.findViewById(id); 121 } 122 123 /** 124 * @see RecyclerMixin#setDividerItemDecoration(DividerItemDecoration) 125 */ setDividerItemDecoration(DividerItemDecoration decoration)126 public void setDividerItemDecoration(DividerItemDecoration decoration) { 127 mRecyclerMixin.setDividerItemDecoration(decoration); 128 } 129 130 /** 131 * @see RecyclerMixin#getRecyclerView() 132 */ getRecyclerView()133 public RecyclerView getRecyclerView() { 134 return mRecyclerMixin.getRecyclerView(); 135 } 136 137 /** 138 * @see RecyclerMixin#setAdapter(Adapter) 139 */ setAdapter(Adapter<? extends ViewHolder> adapter)140 public void setAdapter(Adapter<? extends ViewHolder> adapter) { 141 mRecyclerMixin.setAdapter(adapter); 142 } 143 144 /** 145 * @see RecyclerMixin#getAdapter() 146 */ getAdapter()147 public Adapter<? extends ViewHolder> getAdapter() { 148 return mRecyclerMixin.getAdapter(); 149 } 150 151 /** 152 * @deprecated Use {@link #setDividerInsets(int, int)} instead. 153 */ 154 @Deprecated setDividerInset(int inset)155 public void setDividerInset(int inset) { 156 mRecyclerMixin.setDividerInset(inset); 157 } 158 159 /** 160 * @see RecyclerMixin#setDividerInset(int) 161 */ setDividerInsets(int start, int end)162 public void setDividerInsets(int start, int end) { 163 mRecyclerMixin.setDividerInsets(start, end); 164 } 165 166 /** 167 * @deprecated Use {@link #getDividerInsetStart()} instead. 168 */ 169 @Deprecated getDividerInset()170 public int getDividerInset() { 171 return mRecyclerMixin.getDividerInset(); 172 } 173 174 /** 175 * @see RecyclerMixin#getDividerInsetStart() 176 */ getDividerInsetStart()177 public int getDividerInsetStart() { 178 return mRecyclerMixin.getDividerInsetStart(); 179 } 180 181 /** 182 * @see RecyclerMixin#getDividerInsetEnd() 183 */ getDividerInsetEnd()184 public int getDividerInsetEnd() { 185 return mRecyclerMixin.getDividerInsetEnd(); 186 } 187 188 /** 189 * @see RecyclerMixin#getDivider() 190 */ getDivider()191 public Drawable getDivider() { 192 return mRecyclerMixin.getDivider(); 193 } 194 } 195