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.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.support.v7.widget.RecyclerView; 22 import android.support.v7.widget.RecyclerView.Adapter; 23 import android.support.v7.widget.RecyclerView.ViewHolder; 24 import android.util.AttributeSet; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 29 import com.android.setupwizardlib.template.RecyclerMixin; 30 import com.android.setupwizardlib.template.RecyclerViewScrollHandlingDelegate; 31 import com.android.setupwizardlib.template.RequireScrollMixin; 32 33 /** 34 * A setup wizard layout for use with {@link android.support.v7.widget.RecyclerView}. 35 * {@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 * 38 * @see SetupWizardListLayout 39 */ 40 public class SetupWizardRecyclerLayout extends SetupWizardLayout { 41 42 private static final String TAG = "RecyclerLayout"; 43 44 protected RecyclerMixin mRecyclerMixin; 45 46 public SetupWizardRecyclerLayout(Context context) { 47 this(context, 0, 0); 48 } 49 50 public SetupWizardRecyclerLayout(Context context, int template, int containerId) { 51 super(context, template, containerId); 52 init(context, null, 0); 53 } 54 55 public SetupWizardRecyclerLayout(Context context, AttributeSet attrs) { 56 super(context, attrs); 57 init(context, attrs, 0); 58 } 59 60 public SetupWizardRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr) { 61 super(context, attrs, defStyleAttr); 62 init(context, attrs, defStyleAttr); 63 } 64 65 private void init(Context context, AttributeSet attrs, int defStyleAttr) { 66 mRecyclerMixin.parseAttributes(attrs, defStyleAttr); 67 registerMixin(RecyclerMixin.class, mRecyclerMixin); 68 69 70 final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class); 71 requireScrollMixin.setScrollHandlingDelegate( 72 new RecyclerViewScrollHandlingDelegate(requireScrollMixin, getRecyclerView())); 73 } 74 75 @Override 76 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 77 super.onLayout(changed, left, top, right, bottom); 78 mRecyclerMixin.onLayout(); 79 } 80 81 /** 82 * @see RecyclerMixin#getAdapter() 83 */ 84 public Adapter<? extends ViewHolder> getAdapter() { 85 return mRecyclerMixin.getAdapter(); 86 } 87 88 /** 89 * @see RecyclerMixin#setAdapter(Adapter) 90 */ 91 public void setAdapter(Adapter<? extends ViewHolder> adapter) { 92 mRecyclerMixin.setAdapter(adapter); 93 } 94 95 /** 96 * @see RecyclerMixin#getRecyclerView() 97 */ 98 public RecyclerView getRecyclerView() { 99 return mRecyclerMixin.getRecyclerView(); 100 } 101 102 @Override 103 protected ViewGroup findContainer(int containerId) { 104 if (containerId == 0) { 105 containerId = R.id.suw_recycler_view; 106 } 107 return super.findContainer(containerId); 108 } 109 110 @Override 111 protected View onInflateTemplate(LayoutInflater inflater, int template) { 112 if (template == 0) { 113 template = R.layout.suw_recycler_template; 114 } 115 return super.onInflateTemplate(inflater, template); 116 } 117 118 @Override 119 protected void onTemplateInflated() { 120 final View recyclerView = findViewById(R.id.suw_recycler_view); 121 if (recyclerView instanceof RecyclerView) { 122 mRecyclerMixin = new RecyclerMixin(this, (RecyclerView) recyclerView); 123 } else { 124 throw new IllegalStateException( 125 "SetupWizardRecyclerLayout should use a template with recycler view"); 126 } 127 } 128 129 @Override 130 // Returning generic type is the common pattern used for findViewBy* methods 131 @SuppressWarnings("TypeParameterUnusedInFormals") 132 public <T extends View> T findManagedViewById(int id) { 133 final View header = mRecyclerMixin.getHeader(); 134 if (header != null) { 135 final T view = header.findViewById(id); 136 if (view != null) { 137 return view; 138 } 139 } 140 return super.findViewById(id); 141 } 142 143 /** 144 * @deprecated Use {@link #setDividerInsets(int, int)} instead. 145 */ 146 @Deprecated 147 public void setDividerInset(int inset) { 148 mRecyclerMixin.setDividerInset(inset); 149 } 150 151 /** 152 * Sets the start inset of the divider. This will use the default divider drawable set in the 153 * theme and apply insets to it. 154 * 155 * @param start The number of pixels to inset on the "start" side of the list divider. Typically 156 * this will be either {@code @dimen/suw_items_icon_divider_inset} or 157 * {@code @dimen/suw_items_text_divider_inset}. 158 * @param end The number of pixels to inset on the "end" side of the list divider. 159 * 160 * @see RecyclerMixin#setDividerInsets(int, int) 161 */ 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 170 public int getDividerInset() { 171 return mRecyclerMixin.getDividerInset(); 172 } 173 174 /** 175 * @see RecyclerMixin#getDividerInsetStart() 176 */ 177 public int getDividerInsetStart() { 178 return mRecyclerMixin.getDividerInsetStart(); 179 } 180 181 /** 182 * @see RecyclerMixin#getDividerInsetEnd() 183 */ 184 public int getDividerInsetEnd() { 185 return mRecyclerMixin.getDividerInsetEnd(); 186 } 187 188 /** 189 * @see RecyclerMixin#getDivider() 190 */ 191 public Drawable getDivider() { 192 return mRecyclerMixin.getDivider(); 193 } 194 } 195