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 public View findManagedViewById(int id) { 131 final View header = mRecyclerMixin.getHeader(); 132 if (header != null) { 133 final View view = header.findViewById(id); 134 if (view != null) { 135 return view; 136 } 137 } 138 return super.findViewById(id); 139 } 140 141 /** 142 * @deprecated Use {@link #setDividerInsets(int, int)} instead. 143 */ 144 @Deprecated 145 public void setDividerInset(int inset) { 146 mRecyclerMixin.setDividerInset(inset); 147 } 148 149 /** 150 * Sets the start inset of the divider. This will use the default divider drawable set in the 151 * theme and apply insets to it. 152 * 153 * @param start The number of pixels to inset on the "start" side of the list divider. Typically 154 * this will be either {@code @dimen/suw_items_icon_divider_inset} or 155 * {@code @dimen/suw_items_text_divider_inset}. 156 * @param end The number of pixels to inset on the "end" side of the list divider. 157 * 158 * @see RecyclerMixin#setDividerInsets(int, 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