List9.java revision b39c4ba316a523e4ecf0ac9e71377b8c5b35c25c
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 com.example.android.apis.view;
18
19import com.example.android.apis.R;
20
21import android.app.ListActivity;
22import android.content.Context;
23import android.graphics.PixelFormat;
24import android.os.Bundle;
25import android.os.Handler;
26import android.view.View;
27import android.view.LayoutInflater;
28import android.view.WindowManager;
29import android.view.WindowManager.LayoutParams;
30import android.widget.AbsListView;
31import android.widget.ArrayAdapter;
32import android.widget.ListView;
33import android.widget.TextView;
34
35
36/**
37 * Another variation of the list of cheeses. In this case, we use
38 * {@link AbsListView#setOnScrollListener(AbsListView.OnScrollListener)
39 * AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the
40 * first letter of the visible range of cheeses.
41 */
42public class List9 extends ListActivity implements ListView.OnScrollListener {
43
44    private final class RemoveWindow implements Runnable {
45        public void run() {
46            removeWindow();
47        }
48    }
49
50    private RemoveWindow mRemoveWindow = new RemoveWindow();
51    Handler mHandler = new Handler();
52    private WindowManager mWindowManager;
53    private TextView mDialogText;
54    private boolean mShowing;
55    private boolean mReady;
56    private char mPrevLetter = Character.MIN_VALUE;
57
58    @Override
59    public void onCreate(Bundle savedInstanceState) {
60        super.onCreate(savedInstanceState);
61
62        mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
63
64        // Use an existing ListAdapter that will map an array
65        // of strings to TextViews
66        setListAdapter(new ArrayAdapter<String>(this,
67                android.R.layout.simple_list_item_1, mStrings));
68
69        getListView().setOnScrollListener(this);
70
71        LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
72
73        mDialogText = (TextView) inflate.inflate(R.layout.list_position, null);
74        mDialogText.setVisibility(View.INVISIBLE);
75
76        mHandler.post(new Runnable() {
77
78            public void run() {
79                mReady = true;
80                WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
81                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
82                        WindowManager.LayoutParams.TYPE_APPLICATION,
83                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
84                                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
85                        PixelFormat.TRANSLUCENT);
86                mWindowManager.addView(mDialogText, lp);
87            }});
88    }
89
90    @Override
91    protected void onResume() {
92        super.onResume();
93        mReady = true;
94    }
95
96
97    @Override
98    protected void onPause() {
99        super.onPause();
100        removeWindow();
101        mReady = false;
102    }
103
104    @Override
105    protected void onDestroy() {
106        super.onDestroy();
107        mWindowManager.removeView(mDialogText);
108        mReady = false;
109    }
110
111
112
113
114    public void onScroll(AbsListView view, int firstVisibleItem,
115            int visibleItemCount, int totalItemCount) {
116        if (mReady) {
117            char firstLetter = mStrings[firstVisibleItem].charAt(0);
118
119            if (!mShowing && firstLetter != mPrevLetter) {
120
121                mShowing = true;
122                mDialogText.setVisibility(View.VISIBLE);
123            }
124            mDialogText.setText(((Character)firstLetter).toString());
125            mHandler.removeCallbacks(mRemoveWindow);
126            mHandler.postDelayed(mRemoveWindow, 3000);
127            mPrevLetter = firstLetter;
128        }
129    }
130
131
132    public void onScrollStateChanged(AbsListView view, int scrollState) {
133    }
134
135
136    private void removeWindow() {
137        if (mShowing) {
138            mShowing = false;
139            mDialogText.setVisibility(View.INVISIBLE);
140        }
141    }
142
143    private String[] mStrings = Cheeses.sCheeseStrings;
144}
145