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