1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.qs; 16 17 import android.annotation.Nullable; 18 import android.content.Context; 19 import android.content.res.TypedArray; 20 import android.database.DataSetObserver; 21 import android.os.Handler; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.widget.LinearLayout; 25 import android.widget.ListAdapter; 26 import com.android.systemui.R; 27 28 /** 29 * Similar to a ListView, but it will show only as many items as fit on screen and 30 * bind those instead of scrolling. 31 */ 32 public class AutoSizingList extends LinearLayout { 33 34 private static final String TAG = "AutoSizingList"; 35 private final int mItemSize; 36 private final Handler mHandler; 37 38 private ListAdapter mAdapter; 39 private int mCount; 40 AutoSizingList(Context context, @Nullable AttributeSet attrs)41 public AutoSizingList(Context context, @Nullable AttributeSet attrs) { 42 super(context, attrs); 43 44 mHandler = new Handler(); 45 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoSizingList); 46 mItemSize = a.getDimensionPixelSize(R.styleable.AutoSizingList_itemHeight, 0); 47 } 48 setAdapter(ListAdapter adapter)49 public void setAdapter(ListAdapter adapter) { 50 if (mAdapter != null) { 51 mAdapter.unregisterDataSetObserver(mDataObserver); 52 } 53 mAdapter = adapter; 54 if (adapter != null) { 55 adapter.registerDataSetObserver(mDataObserver); 56 } 57 } 58 59 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)60 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 61 int requestedHeight = MeasureSpec.getSize(heightMeasureSpec); 62 if (requestedHeight != 0) { 63 int count = Math.min(requestedHeight / mItemSize, getDesiredCount()); 64 if (mCount != count) { 65 postRebindChildren(); 66 mCount = count; 67 } 68 } 69 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 70 } 71 getDesiredCount()72 private int getDesiredCount() { 73 return mAdapter != null ? mAdapter.getCount() : 0; 74 } 75 postRebindChildren()76 private void postRebindChildren() { 77 mHandler.post(mBindChildren); 78 } 79 rebindChildren()80 private void rebindChildren() { 81 if (mAdapter == null) { 82 return; 83 } 84 for (int i = 0; i < mCount; i++) { 85 View v = i < getChildCount() ? getChildAt(i) : null; 86 View newView = mAdapter.getView(i, v, this); 87 if (newView != v) { 88 if (v != null) { 89 removeView(v); 90 } 91 addView(newView, i); 92 } 93 } 94 // Ditch extra views. 95 while (getChildCount() > mCount) { 96 removeViewAt(getChildCount() - 1); 97 } 98 } 99 100 private final Runnable mBindChildren = new Runnable() { 101 @Override 102 public void run() { 103 rebindChildren(); 104 } 105 }; 106 107 private final DataSetObserver mDataObserver = new DataSetObserver() { 108 @Override 109 public void onChanged() { 110 if (mCount > getDesiredCount()) { 111 mCount = getDesiredCount(); 112 } 113 postRebindChildren(); 114 } 115 116 @Override 117 public void onInvalidated() { 118 postRebindChildren(); 119 } 120 }; 121 } 122