LargeTemplateView.java revision f27b1ffc67228d73326ec3426fef4c9db75cd6fd
1/* 2 * Copyright 2017 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 17package androidx.slice.widget; 18 19import android.content.Context; 20import android.support.annotation.RestrictTo; 21import android.support.v7.widget.LinearLayoutManager; 22import android.support.v7.widget.RecyclerView; 23import android.util.AttributeSet; 24 25import java.util.ArrayList; 26import java.util.List; 27 28import androidx.slice.Slice; 29import androidx.slice.SliceItem; 30 31/** 32 * @hide 33 */ 34@RestrictTo(RestrictTo.Scope.LIBRARY) 35public class LargeTemplateView extends SliceChildView { 36 37 private final LargeSliceAdapter mAdapter; 38 private final RecyclerView mRecyclerView; 39 private Slice mSlice; 40 private boolean mIsScrollable; 41 private ListContent mListContent; 42 private List<SliceItem> mDisplayedItems = new ArrayList<>(); 43 private int mDisplayedItemsHeight = 0; 44 45 public LargeTemplateView(Context context) { 46 super(context); 47 mRecyclerView = new RecyclerView(getContext()); 48 mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 49 mAdapter = new LargeSliceAdapter(context); 50 mRecyclerView.setAdapter(mAdapter); 51 addView(mRecyclerView); 52 } 53 54 @Override 55 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 56 int height = MeasureSpec.getSize(heightMeasureSpec); 57 if (mDisplayedItems.size() > 0 && mDisplayedItemsHeight > height) { 58 // Need to resize 59 updateDisplayedItems(height); 60 } 61 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 62 } 63 64 @Override 65 public int getActualHeight() { 66 return mDisplayedItemsHeight; 67 } 68 69 @Override 70 public void setTint(int tint) { 71 super.setTint(tint); 72 populate(); 73 } 74 75 @Override 76 public @SliceView.SliceMode int getMode() { 77 return SliceView.MODE_LARGE; 78 } 79 80 @Override 81 public void setSliceActionListener(SliceView.OnSliceActionListener observer) { 82 mObserver = observer; 83 if (mAdapter != null) { 84 mAdapter.setSliceObserver(mObserver); 85 } 86 } 87 88 @Override 89 public void setSliceActions(List<SliceItem> actions) { 90 mAdapter.setSliceActions(actions); 91 } 92 93 @Override 94 public void setSlice(Slice slice) { 95 mSlice = slice; 96 populate(); 97 } 98 99 @Override 100 public void setStyle(AttributeSet attrs) { 101 super.setStyle(attrs); 102 mAdapter.setStyle(attrs); 103 } 104 105 private void populate() { 106 if (mSlice == null) { 107 resetView(); 108 return; 109 } 110 mListContent = new ListContent(getContext(), mSlice); 111 updateDisplayedItems(getMeasuredHeight()); 112 } 113 114 /** 115 * Whether or not the content in this template should be scrollable. 116 */ 117 public void setScrollable(boolean isScrollable) { 118 mIsScrollable = isScrollable; 119 updateDisplayedItems(getMeasuredHeight()); 120 } 121 122 private void updateDisplayedItems(int height) { 123 if (mListContent == null) { 124 return; 125 } 126 if (!mIsScrollable) { 127 // If we're not scrollable we must cap the number of items we're displaying such 128 // that they fit in the available space 129 if (height == 0) { 130 // Not measured, use default 131 mDisplayedItems = mListContent.getItemsForNonScrollingList(-1); 132 } else { 133 mDisplayedItems = mListContent.getItemsForNonScrollingList(height); 134 } 135 } else { 136 mDisplayedItems = mListContent.getRowItems(); 137 } 138 mDisplayedItemsHeight = ListContent.getListHeight(getContext(), mDisplayedItems); 139 mAdapter.setSliceItems(mDisplayedItems, mTintColor); 140 } 141 142 @Override 143 public void resetView() { 144 mSlice = null; 145 mDisplayedItemsHeight = 0; 146 mDisplayedItems.clear(); 147 mAdapter.setSliceItems(null, -1); 148 mListContent = null; 149 } 150} 151