【Android】 实现ListView滑动首字母提示

2012-01-29 9:28
转:http://bigcat.easymorse.com/?p=1501

在ListView快速滚动示例增加首字母提示中,实现了ListView滑动,首字母提示的功能,但是在实际应用中,还存在一些问题.比如从其他Activity切换过来或者切换到其他Activity会存在提示的首字母不消失的问题,这里用另一种方式实现了首字母的提示,并且解决了以上问题,效果如下:

说一下实现方式:

项目目录结构:

其中overlay是提示字母View的布局文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="100dp" android:maxWidth="100dp" android:gravity="center"
    android:textSize="55sp" android:textColor="#ffffffff"
    android:background="@drawable/blue_bg" android:padding="10dp"
    android:layout_margin="8dp" android:visibility="invisible" />

MainActivity中,onCreate方法如下:

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
      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.overlay, 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);
          }});
  }

首先像ListView中填充数据:

setListAdapter(new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, mStrings));

加载首字母提示的View布局:

LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mDialogText = (TextView) inflate.inflate(R.layout.overlay, 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);
          }});

注册滑动监听事件:

getListView().setOnScrollListener(this);

在滑动时间onScroll方法中,修改展示首字母提示:

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, 1000);
          mPrevLetter = firstLetter;
      }

   mHandler.postDelayed(mRemoveWindow, 1000);

延迟一秒执行.项目源码见:http://bigcateasymorse.googlecode.com/svn/trunk/listview_fristletter/

发表评论

电子邮件地址不会被公开。 必填项已用*标注