listview快速滑动搜索(一)activity实现onscrolllistener接口

sina0918 贡献于2014-03-28

作者 Administrator  创建于2014-03-27 02:50:00   修改者User  修改于2014-03-27 02:50:00字数7022

文档摘要:在listview滑动过程中可以看到姓名的首字,实现快速定位。可以先看看效果图 ;importandroid.widget.AbsListView.OnScrollListener;importandroid.widget.ListV
关键词:

 在listview滑动过程中可以看到姓名的首字,实现快速定位。可以先看看效果图  1.首先定义list_popup_char_hint.xml用来显示中间的方块 2.定义main.xml和item.xml用来布局显示列表数据 3.编写activity实现OnScrollListener接口,目前有点小疑问,就是关于scrollState这个状态,如果滑动很慢或则快速滑动过程中去手动停止列表,scrollState不为ListView.OnScrollListener.SCROLL_STATE_IDLE,倒是我有时候不能隐藏那个方块。我的解决方案是采用一个定时器,一定时间后,如果还在则隐藏。代码如下 package com.qsearch.listview.demo; import java.text.Collator; import java.text.RuleBasedCollator; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.graphics.PixelFormat; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import android.widget.TextView; public class QSearchListviewDemoActivity extends Activity implements OnScrollListener{ /** Called when the activity is first created. */ private List contactUserList = new ArrayList(); private ContactAdapter adapter; private ListView listView; private WindowManager windowManager; private TextView txtOverlay; // 用来放在WindowManager中显示提示字符 private Handler handler; private DisapearThread disapearThread; private int scrollState; // 滚动的状态 private class DisapearThread implements Runnable { public void run() { // 避免在1s内,用户再次拖动时提示框又执行隐藏命令。 if (scrollState == ListView.OnScrollListener.SCROLL_STATE_IDLE) { txtOverlay.setVisibility(View.INVISIBLE); } } } public static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, Contacts.HAS_PHONE_NUMBER }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtOverlay = (TextView) LayoutInflater.from(this).inflate(R.layout.list_popup_char_hint, null); // 默认设置为不可见。 txtOverlay.setVisibility(View.INVISIBLE); // 设置WindowManager WindowManager.LayoutParams lp = new WindowManager.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, // 设置为无焦点状态 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, // 半透明效果 PixelFormat.TRANSLUCENT); windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); windowManager.addView(txtOverlay, lp); handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SHOW_LOCKER: if(txtOverlay.isShown()){ Log.e("Anderson", "Handler-scrollState="+scrollState); txtOverlay.setVisibility(View.INVISIBLE); } break; default: super.handleMessage(msg); } } }; disapearThread = new DisapearThread(); listView = (ListView) findViewById(android.R.id.list); //加载本地联系人 List tempusers = new ArrayList(); ContentResolver contentResolver = QSearchListviewDemoActivity.this.getContentResolver(); Cursor cursor01 = contentResolver.query(Contacts.CONTENT_URI, QSearchListviewDemoActivity.CONTACTS_SUMMARY_PROJECTION, null, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); cursor01.moveToFirst(); for (int i = 0; i < cursor01.getCount(); i++) { String name = cursor01.getString(cursor01.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); long id = cursor01.getLong(cursor01.getColumnIndex(ContactsContract.Contacts._ID)); String photoId = cursor01.getString(cursor01.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); String lookupKey = cursor01.getString(cursor01.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); ContactUser su = new ContactUser(); su.setName(name); su.setDisplayName(name); su.setContactId(id + ""); su.setChineseCode(Pinyin.getPinYinHeadChar(name, QSearchListviewDemoActivity.this)); su.setPhotoId(photoId); su.setLookupKey(lookupKey); tempusers.add(su); cursor01.moveToNext(); } cursor01.close(); int size = tempusers.size(); sections = new String[size]; for (int i = 0; i < size; i++) { ContactUser s = tempusers.get(i); String ch = s.getName().substring(0,1); sections[i] = ch; } contactUserList.clear(); contactUserList.addAll(tempusers); adapter = new ContactAdapter(QSearchListviewDemoActivity.this, R.layout.contact_item, contactUserList); listView.setAdapter(adapter); listView.setOnScrollListener(this); } RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(Locale.CHINA); String[] sections; // private String py [] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", // "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", // "X", "Y", "Z" }; @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub Log.e("Anderson", "onScrollStateChanged-scrollState="+scrollState); this.scrollState = scrollState; if (scrollState == ListView.OnScrollListener.SCROLL_STATE_IDLE) { handler.removeCallbacks(disapearThread); // 提示延迟1.5s再消失 handler.postDelayed(disapearThread, THREAD_HIDE_TIME); } else if (scrollState == ListView.OnScrollListener.SCROLL_STATE_FLING) { txtOverlay.setVisibility(View.VISIBLE); if(lockTimer != null) { lockTimer.cancel(); lockTimer.purge(); lockTimer = null; } } else { txtOverlay.setVisibility(View.VISIBLE); if(lockTimer != null) { lockTimer.cancel(); lockTimer.purge(); lockTimer = null; } lockTimer = new Timer(); lockTimer.schedule(new LockTimerTask(), TIMER_HIDE_TIME); } } private final static int SHOW_LOCKER = 0; private final static int THREAD_HIDE_TIME = 1500; private final static int TIMER_HIDE_TIME = 2000; private class LockTimerTask extends TimerTask{ @Override public void run() { handler.sendMessage(handler.obtainMessage(SHOW_LOCKER)); } }; private Timer lockTimer; @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub Log.e("Anderson", " totalItemCount="+totalItemCount+" view.size="+view.getLastVisiblePosition()+" view.getCount="+view.getCount()); if(view.getLastVisiblePosition() == view.getCount()-1){ txtOverlay.setText(sections[totalItemCount - 1]); }else{ txtOverlay.setText(sections[firstVisibleItem + (visibleItemCount>>1)]); } } public void onDestroy() { super.onDestroy(); // 将txtOverlay删除。 txtOverlay.setVisibility(View.INVISIBLE); windowManager.removeView(txtOverlay); if(lockTimer != null) { lockTimer.cancel(); lockTimer.purge(); lockTimer = null; } } } 如有疑问,欢迎留言讨论。需要源码的可留邮箱。

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档