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.google.android.setupdesign; 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.util.AttributeSet; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ListAdapter; 28 import android.widget.ListView; 29 import com.google.android.setupdesign.template.ListMixin; 30 import com.google.android.setupdesign.template.ListViewScrollHandlingDelegate; 31 import com.google.android.setupdesign.template.RequireScrollMixin; 32 33 /** 34 * A GLIF themed layout with a ListView. {@code android:entries} can also be used to specify an 35 * {@link com.google.android.setupdesign.items.ItemHierarchy} to be used with this layout in XML. 36 */ 37 public class GlifListLayout extends GlifLayout { 38 39 private ListMixin listMixin; 40 GlifListLayout(Context context)41 public GlifListLayout(Context context) { 42 this(context, 0, 0); 43 } 44 GlifListLayout(Context context, int template)45 public GlifListLayout(Context context, int template) { 46 this(context, template, 0); 47 } 48 GlifListLayout(Context context, int template, int containerId)49 public GlifListLayout(Context context, int template, int containerId) { 50 super(context, template, containerId); 51 init(null, 0); 52 } 53 GlifListLayout(Context context, AttributeSet attrs)54 public GlifListLayout(Context context, AttributeSet attrs) { 55 super(context, attrs); 56 init(attrs, 0); 57 } 58 59 @TargetApi(VERSION_CODES.HONEYCOMB) GlifListLayout(Context context, AttributeSet attrs, int defStyleAttr)60 public GlifListLayout(Context context, AttributeSet attrs, int defStyleAttr) { 61 super(context, attrs, defStyleAttr); 62 init(attrs, defStyleAttr); 63 } 64 init(AttributeSet attrs, int defStyleAttr)65 private void init(AttributeSet attrs, int defStyleAttr) { 66 listMixin = new ListMixin(this, attrs, defStyleAttr); 67 registerMixin(ListMixin.class, listMixin); 68 69 final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class); 70 requireScrollMixin.setScrollHandlingDelegate( 71 new ListViewScrollHandlingDelegate(requireScrollMixin, getListView())); 72 } 73 74 @Override onLayout(boolean changed, int left, int top, int right, int bottom)75 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 76 super.onLayout(changed, left, top, right, bottom); 77 listMixin.onLayout(); 78 } 79 80 @Override onInflateTemplate(LayoutInflater inflater, int template)81 protected View onInflateTemplate(LayoutInflater inflater, int template) { 82 if (template == 0) { 83 template = R.layout.sud_glif_list_template; 84 } 85 return super.onInflateTemplate(inflater, template); 86 } 87 88 @Override findContainer(int containerId)89 protected ViewGroup findContainer(int containerId) { 90 if (containerId == 0) { 91 containerId = android.R.id.list; 92 } 93 return super.findContainer(containerId); 94 } 95 getListView()96 public ListView getListView() { 97 return listMixin.getListView(); 98 } 99 setAdapter(ListAdapter adapter)100 public void setAdapter(ListAdapter adapter) { 101 listMixin.setAdapter(adapter); 102 } 103 getAdapter()104 public ListAdapter getAdapter() { 105 return listMixin.getAdapter(); 106 } 107 108 /** @deprecated Use {@link #setDividerInsets(int, int)} instead. */ 109 @Deprecated setDividerInset(int inset)110 public void setDividerInset(int inset) { 111 listMixin.setDividerInset(inset); 112 } 113 114 /** 115 * Sets the start inset of the divider. This will use the default divider drawable set in the 116 * theme and apply insets to it. 117 * 118 * @param start The number of pixels to inset on the "start" side of the list divider. Typically 119 * this will be either {@code @dimen/sud_items_glif_icon_divider_inset} or 120 * {@code @dimen/sud_items_glif_text_divider_inset}. 121 * @param end The number of pixels to inset on the "end" side of the list divider. 122 * @see ListMixin#setDividerInsets(int, int) 123 */ setDividerInsets(int start, int end)124 public void setDividerInsets(int start, int end) { 125 listMixin.setDividerInsets(start, end); 126 } 127 128 /** @deprecated Use {@link #getDividerInsetStart()} instead. */ 129 @Deprecated getDividerInset()130 public int getDividerInset() { 131 return listMixin.getDividerInset(); 132 } 133 134 /** @see ListMixin#getDividerInsetStart() */ getDividerInsetStart()135 public int getDividerInsetStart() { 136 return listMixin.getDividerInsetStart(); 137 } 138 139 /** @see ListMixin#getDividerInsetEnd() */ getDividerInsetEnd()140 public int getDividerInsetEnd() { 141 return listMixin.getDividerInsetEnd(); 142 } 143 144 /** @see ListMixin#getDivider() */ getDivider()145 public Drawable getDivider() { 146 return listMixin.getDivider(); 147 } 148 } 149