List9public class List9 extends android.app.ListActivity implements ListView.OnScrollListenerAnother variation of the list of cheeses. In this case, we use
{@link AbsListView#setOnScrollListener(AbsListView.OnScrollListener)
AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the
first letter of the visible range of cheeses. |
Fields Summary |
---|
private RemoveWindow | mRemoveWindow | android.os.Handler | mHandler | private android.view.WindowManager | mWindowManager | private android.widget.TextView | mDialogText | private boolean | mShowing | private boolean | mReady | private char | mPrevLetter | private String[] | mStrings |
Methods Summary |
---|
public void | onCreate(android.os.Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
// Use an existing ListAdapter that will map an array
// of strings to TextViews
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
getListView().setOnScrollListener(this);
LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDialogText = (TextView) inflate.inflate(R.layout.list_position, null);
mDialogText.setVisibility(View.INVISIBLE);
mHandler.post(new Runnable() {
public void run() {
mReady = true;
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mWindowManager.addView(mDialogText, lp);
}});
| protected void | onDestroy()
super.onDestroy();
mWindowManager.removeView(mDialogText);
mReady = false;
| protected void | onPause()
super.onPause();
removeWindow();
mReady = false;
| protected void | onResume()
super.onResume();
mReady = true;
| public void | onScroll(android.widget.AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
int lastItem = firstVisibleItem + visibleItemCount - 1;
if (mReady) {
char firstLetter = mStrings[firstVisibleItem].charAt(0);
if (!mShowing && firstLetter != mPrevLetter) {
mShowing = true;
mDialogText.setVisibility(View.VISIBLE);
}
mDialogText.setText(((Character)firstLetter).toString());
mHandler.removeCallbacks(mRemoveWindow);
mHandler.postDelayed(mRemoveWindow, 3000);
mPrevLetter = firstLetter;
}
| public void | onScrollStateChanged(android.widget.AbsListView view, int scrollState)
| private void | removeWindow()
if (mShowing) {
mShowing = false;
mDialogText.setVisibility(View.INVISIBLE);
}
|
|