1 /* 2 * Copyright (C) 2007 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 android.widget.listview; 18 19 import android.content.Context; 20 import android.util.ListScenario; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.AbsListView; 24 import android.widget.TextView; 25 26 /** 27 * A list where each item expands by 1.5 when selected. 28 */ 29 public class ListItemsExpandOnSelection extends ListScenario { 30 31 32 @Override init(Params params)33 protected void init(Params params) { 34 params.setNumItems(10) 35 .setItemScreenSizeFactor(1.0/5); 36 } 37 38 39 @Override createView(int position, ViewGroup parent, int desiredHeight)40 protected View createView(int position, ViewGroup parent, int desiredHeight) { 41 TextView result = new ExpandWhenSelectedView(parent.getContext(), desiredHeight); 42 result.setHeight(desiredHeight); 43 result.setFocusable(mItemsFocusable); 44 result.setText(getValueAtPosition(position)); 45 final AbsListView.LayoutParams lp = new AbsListView.LayoutParams( 46 ViewGroup.LayoutParams.MATCH_PARENT, 47 ViewGroup.LayoutParams.WRAP_CONTENT); 48 result.setLayoutParams(lp); 49 return result; 50 } 51 52 53 @Override convertView(int position, View convertView, ViewGroup parent)54 public View convertView(int position, View convertView, ViewGroup parent) { 55 ((ExpandWhenSelectedView)convertView).setText(getValueAtPosition(position)); 56 return convertView; 57 } 58 59 60 static private class ExpandWhenSelectedView extends TextView { 61 62 private final int mDesiredHeight; 63 ExpandWhenSelectedView(Context context, int desiredHeight)64 public ExpandWhenSelectedView(Context context, int desiredHeight) { 65 super(context); 66 mDesiredHeight = desiredHeight; 67 } 68 69 @Override setSelected(boolean selected)70 public void setSelected(boolean selected) { 71 super.setSelected(selected); 72 if (selected) { 73 setHeight((int) (mDesiredHeight * 1.5)); 74 } else { 75 setHeight(mDesiredHeight); 76 } 77 } 78 } 79 } 80