package com.qsearch.listview.demo;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
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.AsyncTask;
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<ContactUser> contactUserList = new ArrayList<ContactUser>();
private ContactFriendBo contactFriendBo;
private ContactAdapter adapter;
private ListView listView;
private MyView myView;
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);
contactFriendBo = new ContactFriendBo(this);
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;
case SHOW_VIEW_PY:
listView.setOnScrollListener(null);
int localObject = msg.getData().getInt("localObject", -1);
if(localObject >= 0 && localObject < 26){
txtOverlay.setText(py[localObject]);
Log.e("Anderson", "py[localObject]="+py[localObject]+" || "+sectionMap.get(py[localObject])+" || "+sectionMap.size());
if(sectionMap.get(py[localObject]) != null)
listView.setSelection(sectionMap.get(py[localObject]).intValue());
txtOverlay.setVisibility(View.VISIBLE);
}else{
txtOverlay.setVisibility(View.INVISIBLE);
}
break;
case SHOW_VIEW_UP:
txtOverlay.setVisibility(View.INVISIBLE);
listView.setOnScrollListener(QSearchListviewDemoActivity.this);
break;
default:
super.handleMessage(msg);
}
}
};
disapearThread = new DisapearThread();
listView = (ListView) findViewById(android.R.id.list);
myView = (MyView) findViewById(R.id.myview);
myView.setHandler(handler);
adapter = new ContactAdapter(QSearchListviewDemoActivity.this, R.layout.contact_item, contactUserList);
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
new FirstLoadTask().execute();
}
class FirstLoadTask extends AsyncTask<Object, Object, Object>{
List<ContactUser> tempusers = new ArrayList<ContactUser>();
@Override
protected void onPreExecute() {
}
protected void onProgressUpdate(Integer... values) {
}
@Override
protected Object doInBackground(Object... arg0) {
// TODO Auto-generated method stub
//加载本地联系人
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();
contactFriendBo.initQzContactFriendData(tempusers);
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);
String headPy = s.getChineseCode().substring(0,1).toUpperCase();
if(sectionMap.get(headPy) == null){
sectionMap.put(headPy, i);
}
sections[i] = ch;
}
return true;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
protected void onPostExecute(Object result) {
contactUserList.clear();
contactUserList.addAll(tempusers);
adapter.notifyDataSetChanged();
}
}
RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(Locale.CHINA);
String[] sections;
Map<String, Integer> sectionMap = new HashMap<String, Integer>();
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;
}
lo
- 1
- 2
前往页