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 androidx.recyclerview.widget.RecyclerView; 24 import androidx.recyclerview.widget.RecyclerView.Adapter; 25 import androidx.recyclerview.widget.RecyclerView.ViewHolder; 26 import android.util.AttributeSet; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import com.android.setupwizardlib.template.RecyclerMixin; 31 import com.android.setupwizardlib.template.RecyclerViewScrollHandlingDelegate; 32 import com.android.setupwizardlib.template.RequireScrollMixin; 33 34 /** 35 * A GLIF themed layout with a RecyclerView. {@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 public class GlifRecyclerLayout extends GlifLayout { 39 40 protected RecyclerMixin mRecyclerMixin; 41 GlifRecyclerLayout(Context context)42 public GlifRecyclerLayout(Context context) { 43 this(context, 0, 0); 44 } 45 GlifRecyclerLayout(Context context, int template)46 public GlifRecyclerLayout(Context context, int template) { 47 this(context, template, 0); 48 } 49 GlifRecyclerLayout(Context context, int template, int containerId)50 public GlifRecyclerLayout(Context context, int template, int containerId) { 51 super(context, template, containerId); 52 init(null, 0); 53 } 54 GlifRecyclerLayout(Context context, AttributeSet attrs)55 public GlifRecyclerLayout(Context context, AttributeSet attrs) { 56 super(context, attrs); 57 init(attrs, 0); 58 } 59 60 @TargetApi(VERSION_CODES.HONEYCOMB) GlifRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr)61 public GlifRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr) { 62 super(context, attrs, defStyleAttr); 63 init(attrs, defStyleAttr); 64 } 65 init(AttributeSet attrs, int defStyleAttr)66 private void init(AttributeSet attrs, int defStyleAttr) { 67 mRecyclerMixin.parseAttributes(attrs, defStyleAttr); 68 registerMixin(RecyclerMixin.class, mRecyclerMixin); 69 70 final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class); 71 requireScrollMixin.setScrollHandlingDelegate( 72 new RecyclerViewScrollHandlingDelegate(requireScrollMixin, getRecyclerView())); 73 } 74 75 @Override onLayout(boolean changed, int left, int top, int right, int bottom)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 @Override onInflateTemplate(LayoutInflater inflater, int template)82 protected View onInflateTemplate(LayoutInflater inflater, int template) { 83 if (template == 0) { 84 template = R.layout.suw_glif_recycler_template; 85 } 86 return super.onInflateTemplate(inflater, template); 87 } 88 89 @Override onTemplateInflated()90 protected void onTemplateInflated() { 91 final View recyclerView = findViewById(R.id.suw_recycler_view); 92 if (recyclerView instanceof RecyclerView) { 93 mRecyclerMixin = new RecyclerMixin(this, (RecyclerView) recyclerView); 94 } else { 95 throw new IllegalStateException( 96 "GlifRecyclerLayout should use a template with recycler view"); 97 } 98 } 99 100 @Override findContainer(int containerId)101 protected ViewGroup findContainer(int containerId) { 102 if (containerId == 0) { 103 containerId = R.id.suw_recycler_view; 104 } 105 return super.findContainer(containerId); 106 } 107 108 @Override 109 // Returning generic type is the common pattern used for findViewBy* methods 110 @SuppressWarnings("TypeParameterUnusedInFormals") findManagedViewById(int id)111 public <T extends View> T findManagedViewById(int id) { 112 final View header = mRecyclerMixin.getHeader(); 113 if (header != null) { 114 final T view = header.findViewById(id); 115 if (view != null) { 116 return view; 117 } 118 } 119 return super.findViewById(id); 120 } 121 122 /** @see RecyclerMixin#setDividerItemDecoration(DividerItemDecoration) */ setDividerItemDecoration(DividerItemDecoration decoration)123 public void setDividerItemDecoration(DividerItemDecoration decoration) { 124 mRecyclerMixin.setDividerItemDecoration(decoration); 125 } 126 127 /** @see RecyclerMixin#getRecyclerView() */ getRecyclerView()128 public RecyclerView getRecyclerView() { 129 return mRecyclerMixin.getRecyclerView(); 130 } 131 132 /** @see RecyclerMixin#setAdapter(Adapter) */ setAdapter(Adapter<? extends ViewHolder> adapter)133 public void setAdapter(Adapter<? extends ViewHolder> adapter) { 134 mRecyclerMixin.setAdapter(adapter); 135 } 136 137 /** @see RecyclerMixin#getAdapter() */ getAdapter()138 public Adapter<? extends ViewHolder> getAdapter() { 139 return mRecyclerMixin.getAdapter(); 140 } 141 142 /** @deprecated Use {@link #setDividerInsets(int, int)} instead. */ 143 @Deprecated setDividerInset(int inset)144 public void setDividerInset(int inset) { 145 mRecyclerMixin.setDividerInset(inset); 146 } 147 148 /** @see RecyclerMixin#setDividerInset(int) */ setDividerInsets(int start, int end)149 public void setDividerInsets(int start, int end) { 150 mRecyclerMixin.setDividerInsets(start, end); 151 } 152 153 /** @deprecated Use {@link #getDividerInsetStart()} instead. */ 154 @Deprecated getDividerInset()155 public int getDividerInset() { 156 return mRecyclerMixin.getDividerInset(); 157 } 158 159 /** @see RecyclerMixin#getDividerInsetStart() */ getDividerInsetStart()160 public int getDividerInsetStart() { 161 return mRecyclerMixin.getDividerInsetStart(); 162 } 163 164 /** @see RecyclerMixin#getDividerInsetEnd() */ getDividerInsetEnd()165 public int getDividerInsetEnd() { 166 return mRecyclerMixin.getDividerInsetEnd(); 167 } 168 169 /** @see RecyclerMixin#getDivider() */ getDivider()170 public Drawable getDivider() { 171 return mRecyclerMixin.getDivider(); 172 } 173 } 174