Android實現系統連絡人字元分組以及字母表導航效果 .

來源:互聯網
上載者:User

  功能的實現結合了網上很多非常的好的實現,小小的最佳化看下。

實現思路:

1. 擷取手機連絡人清單:通過Uri uri = Uri.parse("content://com.android.contacts/data/phones");  查詢連絡人清單

2 連絡人的欄位非常的多,摘取了NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";姓名,電話號碼,以及非常重要的sort_key 欄位,

      在之前做項目,而是通過匯入的拼音包轉化的,這是看網上學的,非常的不錯。

3 得到聯絡列表,則通過listview顯示,顯示的時候進行A--Z分組顯示, 這個也好辦,因為通過sort_key已經得到了從A--Z的排序,只要判斷是否屬於同一字元下,是則不顯示,不是則顯示,同時把該條設定分組標題

4 安字母分組顯示完全後,發現連絡人一多,定位到某一個分組下也很大的力氣,上面加上一個搜尋的話,又懶得輸入,因為手機嘛,操作不變,如果能選擇,並且直接定位到

該分組下面,那就容易了,因為人再懶,讓你去摸一下還是不費力氣的,並且體驗的效果也不錯

所以廢話不多說,就是在右邊需要一個A--z豎直排列的字母表,能選擇,並且能夠定位到該字母的分組。

定位到分組這還容易,用為listview為咋門提供了方法setSelection(postion)就ok 能定位了

 

關鍵是實現右邊的點擊,並且知道你點擊的字母:

5 。 首先是布局,布局方式很多,直接幀布局把他定義在右邊,但發現相對布局也行,而且更簡單

 

 

 

 

 

 

 

 

 

 

package com.droid;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.regex.Pattern;import android.app.Activity;import android.content.AsyncQueryHandler;import android.content.ContentResolver;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.graphics.PixelFormat;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.view.WindowManager;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import com.droid.MyLetterListView.OnTouchingLetterChangedListener;public class ContactList extends Activity {private BaseAdapter adapter;      private ListView personList;    private TextView overlay;    private MyLetterListView letterListView;    private AsyncQueryHandler asyncQuery;      private static final String NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";    private HashMap<String, Integer> alphaIndexer;//存放存在的漢語拼音首字母和與之對應的列表位置    private String[] sections;//存放存在的漢語拼音首字母    private Handler handler;    private OverlayThread overlayThread;      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);                  personList = (ListView) findViewById(R.id.list_view);        letterListView = (MyLetterListView) findViewById(R.id.MyLetterListView01);        letterListView.setOnTouchingLetterChangedListener(new LetterListViewListener());                asyncQuery = new MyAsyncQueryHandler(getContentResolver());        alphaIndexer = new HashMap<String, Integer>();        handler = new Handler();        overlayThread = new OverlayThread();        initOverlay();    }        @Override      protected void onResume() {          super.onResume();          Uri uri = Uri.parse("content://com.android.contacts/data/phones");          String[] projection = { "_id", "display_name", "data1", "sort_key" };          asyncQuery.startQuery(0, null, uri, projection, null, null,                  "sort_key COLLATE LOCALIZED asc");           }        //非同步查詢連絡人    private class MyAsyncQueryHandler extends AsyncQueryHandler {            public MyAsyncQueryHandler(ContentResolver cr) {              super(cr);            }            @Override          protected void onQueryComplete(int token, Object cookie, Cursor cursor) {              if (cursor != null && cursor.getCount() > 0) {                  List<ContentValues> list = new ArrayList<ContentValues>();                  cursor.moveToFirst();                  for (int i = 0; i < cursor.getCount(); i++) {                    ContentValues cv = new ContentValues();                      cursor.moveToPosition(i);                      String name = cursor.getString(1);                      String number = cursor.getString(2);                      String sortKey = cursor.getString(3);                    System.out.println(sortKey);                    if (number.startsWith("+86")) {                          cv.put(NAME, name);                          cv.put(NUMBER, number.substring(3));  //去掉+86                        cv.put(SORT_KEY, sortKey);                      } else {                          cv.put(NAME, name);                          cv.put(NUMBER, number);                          cv.put(SORT_KEY, sortKey);                      }                      list.add(cv);                  }                  if (list.size() > 0) {                      setAdapter(list);                  }              }          }        }        private void setAdapter(List<ContentValues> list) {    adapter = new ListAdapter(this, list);        personList.setAdapter(adapter);        }        private class ListAdapter extends BaseAdapter {     private LayoutInflater inflater;           private List<ContentValues> list;        public ListAdapter(Context context, List<ContentValues> list) {    this.inflater = LayoutInflater.from(context);    this.list = list;    alphaIndexer = new HashMap<String, Integer>();    sections = new String[list.size()];        for (int i = 0; i < list.size(); i++) {    //當前漢語拼音首字母    String currentStr = getAlpha(list.get(i).getAsString(SORT_KEY));    //上一個漢語拼音首字母,如果不存在為“ ”                String previewStr = (i - 1) >= 0 ? getAlpha(list.get(i - 1).getAsString(SORT_KEY)) : " ";                if (!previewStr.equals(currentStr)) {                String name = getAlpha(list.get(i).getAsString(SORT_KEY));                alphaIndexer.put(name, i);                  sections[i] = name;                 }            }    }    @Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if (convertView == null) {                  convertView = inflater.inflate(R.layout.list_item, null);                holder = new ViewHolder();                  holder.alpha = (TextView) convertView.findViewById(R.id.alpha);                  holder.name = (TextView) convertView.findViewById(R.id.name);                  holder.number = (TextView) convertView.findViewById(R.id.number);                  convertView.setTag(holder);              } else {                  holder = (ViewHolder) convertView.getTag();              }              ContentValues cv = list.get(position);              holder.name.setText(cv.getAsString(NAME));            holder.number.setText(cv.getAsString(NUMBER));            String currentStr = getAlpha(list.get(position).getAsString(SORT_KEY));//當前字母            String previewStr = (position - 1) >= 0 ? getAlpha(list.get(position - 1).getAsString(SORT_KEY)) : " ";            if (!previewStr.equals(currentStr)) {                  holder.alpha.setVisibility(View.VISIBLE);                holder.alpha.setText(currentStr);            } else {                  holder.alpha.setVisibility(View.GONE);            }              return convertView;  }private class ViewHolder {TextView alpha;              TextView name;              TextView number;}        }        //初始化漢語拼音首字母彈出提示框    private void initOverlay() {    LayoutInflater inflater = LayoutInflater.from(this);    overlay = (TextView) inflater.inflate(R.layout.overlay, null);    overlay.setVisibility(View.INVISIBLE);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 = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);windowManager.addView(overlay, lp);    }        private class LetterListViewListener implements OnTouchingLetterChangedListener{@Overridepublic void onTouchingLetterChanged(final String s) {if(alphaIndexer.get(s) != null) {int position = alphaIndexer.get(s);personList.setSelection(position);overlay.setText(sections[position]);overlay.setVisibility(View.VISIBLE);handler.removeCallbacks(overlayThread);//延遲一秒後執行,讓overlay為不可見handler.postDelayed(overlayThread, 1500);} }        }        //設定overlay不可見    private class OverlayThread implements Runnable {@Overridepublic void run() {overlay.setVisibility(View.GONE);}        }        //獲得漢語拼音首字母    private String getAlpha(String str) {          if (str == null) {              return "#";          }            if (str.trim().length() == 0) {              return "#";          }            char c = str.trim().substring(0, 1).charAt(0);          // Regex,判斷首字母是否是英文字母          Pattern pattern = Pattern.compile("^[A-Za-z]+{1}quot;);          if (pattern.matcher(c + "").matches()) {              return (c + "").toUpperCase();          } else {              return "#";          }      }    }  

 

package com.droid;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Typeface;import android.text.style.TypefaceSpan;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;public class MyLetterListView extends View {OnTouchingLetterChangedListener onTouchingLetterChangedListener;String[] b = {"#","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"};int choose = -1;Paint paint = new Paint();boolean showBkg = false;public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public MyLetterListView(Context context, AttributeSet attrs) {super(context, attrs);}public MyLetterListView(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if(showBkg){    canvas.drawColor(Color.parseColor("#40000000"));}    int height = getHeight();    int width = getWidth();    int singleHeight = height / b.length;    for(int i=0;i<b.length;i++){       paint.setColor(Color.WHITE);       paint.setTypeface(Typeface.DEFAULT_BOLD);       paint.setAntiAlias(true);       if(i == choose){       paint.setColor(Color.parseColor("#3399ff"));       paint.setFakeBoldText(true);       }       float xPos = width/2  - paint.measureText(b[i])/2;       float yPos = singleHeight * i + singleHeight;       canvas.drawText(b[i], xPos, yPos, paint);       paint.reset();    }   }@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {final int action = event.getAction();    final float y = event.getY();    final int oldChoose = choose;    final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;    final int c = (int) (y/getHeight()*b.length);    switch (action) {case MotionEvent.ACTION_DOWN:showBkg = true;if(oldChoose != c && listener != null){if(c > 0 && c< b.length){listener.onTouchingLetterChanged(b[c]);choose = c;invalidate();}}break;case MotionEvent.ACTION_MOVE:if(oldChoose != c && listener != null){if(c > 0 && c< b.length){listener.onTouchingLetterChanged(b[c]);choose = c;invalidate();}}break;case MotionEvent.ACTION_UP:showBkg = false;choose = -1;invalidate();break;}return true;}@Overridepublic boolean onTouchEvent(MotionEvent event) {return super.onTouchEvent(event);}public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;}public interface OnTouchingLetterChangedListener{public void onTouchingLetterChanged(String s);}}

 

<?xml version="1.0" encoding="utf-8"?><RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"><TextViewandroid:id="@+id/alpha"android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:paddingLeft="13dip"  android:background="#333333"android:textColor="#99CCFF"android:textAppearance="?android:textAppearanceMedium"android:visibility="gone" /><ImageViewandroid:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_marginRight="5.0dip"android:src="@drawable/contact_list_icon"android:layout_below="@id/alpha" />    <TextView    android:id="@+id/name"    android:textAppearance="?android:textAppearanceMedium"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="2.0dip"    android:layout_marginTop="6.0dip"    android:layout_marginRight="5.0dip"    android:singleLine="true"    android:layout_toRightOf="@id/image_view"    android:layout_alignTop="@id/image_view" />        <TextView    android:id="@+id/number"    android:textAppearance="?android:textAppearanceSmall"    android:ellipsize="marquee"    android:layout_width="wrap_content"    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent" >          <ListView android:id="@+id/list_view" android:layout_height="wrap_content" android:layout_width="fill_parent"android:scrollbars="none"android:cacheColorHint="#00000000" />    <com.droid.MyLetterListView     android:id="@+id/MyLetterListView01" android:background="#40000000" android:layout_width="30dip" android:layout_height="fill_parent"android:layout_alignParentRight="true" /></RelativeLayout> 

android:singleLine="true" android:textColor="#AAAAAA" android:layout_below="@id/name" android:layout_alignLeft="@id/name" android:layout_alignWithParentIfMissing="true" /> </RelativeLayout>

 

<TextView  xmlns:android="http://schemas.android.com/apk/res/android"  android:textSize="70sp"    android:textColor="#3399ff"    android:background="#ffffff"      android:minWidth="80dip"      android:maxWidth="80dip"      android:padding="5dip"    android:gravity="center" />

這是三個布局檔案,到點了先回去了,沒看懂的可以留言
list_item.xml
main.xml
overlay.xml

  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.