技术联盟

android仿通讯录

friend.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical” >
  6.     <RelativeLayout
  7.         android:layout_width=“fill_parent”
  8.         android:layout_height=“fill_parent”
  9.         android:orientation=“vertical” >
  10.         <ListView
  11.             android:id=“@+id/list_view”
  12.             android:layout_width=“fill_parent”
  13.             android:layout_height=“wrap_content”
  14.             android:scrollbars=“none” >
  15.         </ListView>
  16.         <com.example.menu.MyLetterListView
  17.             android:id=“@+id/my_list_view”
  18.             android:layout_width=“30dip”
  19.             android:layout_height=“fill_parent”
  20.             android:layout_alignParentRight=“true”
  21.              />
  22.     </RelativeLayout>
  23. </LinearLayout>

friend_header.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“fill_parent”
  4.     android:layout_height=“wrap_content”
  5.     android:gravity=“center_vertical”
  6.     android:orientation=“horizontal”
  7.     android:paddingLeft=“2dip”
  8.     android:paddingRight=“2dip” >
  9.     <TextView
  10.         android:id=“@+id/friend_search_head_title”
  11.         android:layout_width=“wrap_content”
  12.         android:layout_height=“wrap_content”
  13.         android:layout_gravity=“center”
  14.         android:clickable=“true”
  15.         android:textSize=“20sp” >
  16.     </TextView>
  17.     <ImageView
  18.         android:id=“@+id/friend_center_back”
  19.         android:layout_width=“wrap_content”
  20.         android:layout_height=“wrap_content”
  21.         android:layout_gravity=“right|center”
  22.         android:clickable=“true” >
  23.     </ImageView>
  24. </FrameLayout>

list_item.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent” >
  5.     <TextView
  6.         android:id=“@+id/alpha”
  7.         android:layout_width=“fill_parent”
  8.         android:layout_height=“wrap_content”
  9.         android:background=“#333333”
  10.         android:paddingLeft=“10dip”
  11.         android:textColor=“#FFFFFF”
  12.         android:visibility=“gone” />
  13.     <ImageView
  14.         android:id=“@+id/imageView”
  15.         android:layout_width=“wrap_content”
  16.         android:layout_height=“wrap_content”
  17.         android:layout_alignParentLeft=“true”
  18.         android:background=“@drawable/contact_list_icon”
  19.         android:layout_below=“@id/alpha” />
  20.     <View
  21.         android:id=“@+id/divider”
  22.         android:layout_width=“1.0dip”
  23.         android:layout_height=“fill_parent”
  24.         android:layout_below=“@id/alpha”
  25.         android:layout_marginRight=“11.0dip”
  26.         android:layout_toRightOf=“@id/imageView” />
  27.     <TextView
  28.         android:id=“@+id/name”
  29.         android:layout_width=“wrap_content”
  30.         android:layout_height=“wrap_content”
  31.         android:layout_alignTop=“@id/divider”
  32.         android:layout_marginLeft=“2.0dip”
  33.         android:layout_marginRight=“5.0dip”
  34.         android:layout_marginTop=“6.0dip”
  35.         android:layout_toRightOf=“@id/divider”
  36.         android:singleLine=“true”
  37.         android:textAppearance=“?android:textAppearanceMedium” />
  38.     <TextView
  39.         android:id=“@+id/number”
  40.         android:layout_width=“wrap_content”
  41.         android:layout_height=“wrap_content”
  42.         android:layout_alignLeft=“@id/name”
  43.         android:layout_alignWithParentIfMissing=“true”
  44.         android:layout_below=“@id/name”
  45.         android:ellipsize=“marquee”
  46.         android:singleLine=“true”
  47.         android:textAppearance=“?android:textAppearanceSmall” />
  48. </RelativeLayout>

overlay.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical” >
  6. </TextView>

suspend_search.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <ImageView xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical” >
  6. </ImageView>

Friend.java

  1. package com.example.menu;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.regex.Pattern;
  6. import com.example.menu.MyLetterListView.OnTouchingLetterChangedListener;
  7. import android.app.Activity;
  8. import android.content.AsyncQueryHandler;
  9. import android.content.ComponentName;
  10. import android.content.ContentResolver;
  11. import android.content.ContentValues;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.database.Cursor;
  15. import android.graphics.PixelFormat;
  16. import android.net.Uri;
  17. import android.os.Bundle;
  18. import android.os.Handler;
  19. import android.provider.ContactsContract;
  20. import android.util.Log;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.view.ViewGroup;
  25. import android.view.Window;
  26. import android.view.WindowManager;
  27. import android.widget.AbsListView;
  28. import android.widget.AbsListView.OnScrollListener;
  29. import android.widget.BaseAdapter;
  30. import android.widget.ImageView;
  31. import android.widget.ListView;
  32. import android.widget.TextView;
  33. import android.widget.Toast;
  34. public class Friend extends Activity
  35. {
  36.     private BaseAdapter adapter;
  37.     private ListView listview;
  38.     private TextView overlay;
  39.     private ImageView suspend_search;
  40.     private MyLetterListView letterListView;
  41.     private AsyncQueryHandler asyncQuery;
  42.     private static final String NAME = “name”, NUMBER = “number”,
  43.             SORT_KEY = “sort_key”;
  44.     private HashMap<String, Integer> alphaIndexer;
  45.     private String[] sections;
  46.     public List<ContentValues> list = new ArrayList<ContentValues>();
  47.     private WindowManager windowManager;
  48.     @Override
  49.     public void onCreate(Bundle savedInstanceState)
  50.     {
  51.         super.onCreate(savedInstanceState);
  52.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  53.         setContentView(R.layout.friend);
  54.         windowManager =
  55.                 (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
  56.         asyncQuery = new MyAsyncQueryHandler(getContentResolver());
  57.         listview = (ListView) findViewById(R.id.list_view);
  58.         letterListView = (MyLetterListView) findViewById(R.id.my_list_view);
  59.         letterListView
  60.                 .setOnTouchingLetterChangedListener(new LetterListViewListener());
  61.         alphaIndexer = new HashMap<String, Integer>();
  62.         new Handler();
  63.         new OverlayThread();
  64.         initOverlay();
  65.         initSuSearch();
  66.         if (list.size() > 0)
  67.         {
  68.         }
  69.         listview.setOnScrollListener(new OnScrollListener()
  70.         {
  71.             @Override
  72.             public void onScrollStateChanged(AbsListView view, int scrollState)
  73.             {
  74.                 suspend_search.setVisibility(View.VISIBLE);
  75.             }
  76.             @Override
  77.             public void onScroll(AbsListView view, int firstVisibleItem,
  78.                     int visibleItemCount, int totalItemCount)
  79.             {
  80.                 suspend_search.setVisibility(View.GONE);
  81.             }
  82.         });
  83.         suspend_search.setOnClickListener(new OnClickListener()
  84.         {
  85.             @Override
  86.             public void onClick(View v)
  87.             {
  88.                 ComponentName friendcName =
  89.                         new ComponentName(Friend.this,
  90.                                 “com.example.test_intent.FriendSearch”);
  91.                 Intent friend_viewIntent = new Intent();
  92.                 friend_viewIntent.setComponent(friendcName);
  93.                 startActivity(friend_viewIntent);
  94.                 Toast.makeText(getApplicationContext(), “sousuo”,
  95.                         Toast.LENGTH_LONG).show();
  96.                 // TODO Auto-generated method stub
  97.             }
  98.         });
  99.     }
  100.     @SuppressWarnings(“deprecation”)
  101.     public void getContent()
  102.     {
  103.         Cursor cur =
  104.                 getContentResolver().query(
  105.                         ContactsContract.Contacts.CONTENT_URI, nullnull,
  106.                         nullnull);
  107.         startManagingCursor(cur);
  108.     }
  109.     @Override
  110.     protected void onResume()
  111.     {
  112.         super.onResume();
  113.         Uri uri = Uri.parse(“content://com.android.contacts/data/phones”);
  114.         String[] projection = { “_id”“display_name”“data1”“sort_key” };
  115.         asyncQuery.startQuery(0null, uri, projection, nullnull,
  116.                 “sort_key COLLATE LOCALIZED asc”);
  117.     }
  118.     private class MyAsyncQueryHandler extends AsyncQueryHandler
  119.     {
  120.         public MyAsyncQueryHandler(ContentResolver cr)
  121.         {
  122.             super(cr);
  123.         }
  124.         @Override
  125.         protected void onQueryComplete(int token, Object cookie, Cursor cursor)
  126.         {
  127.             cursor.moveToFirst();
  128.             Log.d(“ccccc”,
  129.                     cursor.getString(0) + ” 000 “ + cursor.getString(1)
  130.                             + ” 000 “ + cursor.getString(2) + ” 000 “
  131.                             + cursor.getString(3));
  132.             while (cursor.moveToNext())
  133.             {
  134.                 ContentValues cv = new ContentValues();
  135.                 cv.put(NAME, cursor.getString(1));
  136.                 cv.put(NUMBER, cursor.getString(2));
  137.                 cv.put(SORT_KEY, cursor.getString(3));
  138.                 list.add(cv);
  139.             }
  140.             if (list.size() > 0)
  141.             {
  142.                 setAdapter(list);
  143.             }
  144.         }
  145.     }
  146.     private void setAdapter(List<ContentValues> list)
  147.     {
  148.         adapter = new ListAdapter(this, list);
  149.         listview.setAdapter(adapter);
  150.     }
  151.     private class ListAdapter extends BaseAdapter
  152.     {
  153.         private LayoutInflater inflater;
  154.         private List<ContentValues> list;
  155.         public ListAdapter(Context context, List<ContentValues> list)
  156.         {
  157.             this.inflater = LayoutInflater.from(context);
  158.             this.list = list;
  159.             alphaIndexer = new HashMap<String, Integer>();
  160.             sections = new String[list.size()];
  161.             for (int i = 0; i < list.size(); i++)
  162.             {
  163.                 String currentStr = getAlpha(list.get(i).getAsString(SORT_KEY));
  164.                 String previewStr =
  165.                         (i – 1) >= 0 ? getAlpha(list.get(i – 1).getAsString(
  166.                                 SORT_KEY)) : ” “;
  167.                 if (!previewStr.equals(currentStr))
  168.                 {
  169.                     String name = getAlpha(list.get(i).getAsString(SORT_KEY));
  170.                     alphaIndexer.put(name, i);
  171.                     sections[i] = name;
  172.                 }
  173.             }
  174.         }
  175.         @Override
  176.         public int getCount()
  177.         {
  178.             return list.size();
  179.         }
  180.         @Override
  181.         public Object getItem(int position)
  182.         {
  183.             return list.get(position);
  184.         }
  185.         @Override
  186.         public long getItemId(int position)
  187.         {
  188.             return position;
  189.         }
  190.         @Override
  191.         public View getView(int position, View convertView, ViewGroup parent)
  192.         {
  193.             ViewHolder holder;
  194.             if (convertView == null)
  195.             {
  196.                 convertView = inflater.inflate(R.layout.list_item, null);
  197.                 holder = new ViewHolder();
  198.                 holder.alpha = (TextView) convertView.findViewById(R.id.alpha);
  199.                 holder.name = (TextView) convertView.findViewById(R.id.name);
  200.                 holder.number =
  201.                         (TextView) convertView.findViewById(R.id.number);
  202.                 convertView.setTag(holder);
  203.             }
  204.             else
  205.             {
  206.                 holder = (ViewHolder) convertView.getTag();
  207.             }
  208.             ContentValues cv = list.get(position);
  209.             holder.name.setText(cv.getAsString(NAME));
  210.             holder.number.setText(cv.getAsString(NUMBER));
  211.             String currentStr =
  212.                     getAlpha(list.get(position).getAsString(SORT_KEY));
  213.             String previewStr =
  214.                     (position – 1) >= 0 ? getAlpha(list.get(position – 1)
  215.                             .getAsString(SORT_KEY)) : ” “;
  216.             if (!previewStr.equals(currentStr))
  217.             {
  218.                 holder.alpha.setVisibility(View.VISIBLE);
  219.                 holder.alpha.setText(currentStr);
  220.             }
  221.             else
  222.             {
  223.                 holder.alpha.setVisibility(View.GONE);
  224.             }
  225.             return convertView;
  226.         }
  227.         private class ViewHolder
  228.         {
  229.             TextView alpha;
  230.             TextView name;
  231.             TextView number;
  232.         }
  233.     }
  234.     private void initSuSearch()// 搜索
  235.     {
  236.         LayoutInflater inflater = LayoutInflater.from(this);
  237.         suspend_search =
  238.                 (ImageView) inflater.inflate(R.layout.suspend_search, null);
  239.         WindowManager.LayoutParams lp =
  240.                 new WindowManager.LayoutParams(8080170, –280,
  241.                         WindowManager.LayoutParams.TYPE_APPLICATION,
  242.                         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
  243.                         PixelFormat.TRANSLUCENT);
  244.         windowManager.addView(suspend_search, lp);
  245.     }
  246.     private void initOverlay()
  247.     {
  248.         LayoutInflater inflater = LayoutInflater.from(this);
  249.         overlay = (TextView) inflater.inflate(R.layout.overlay, null);
  250.         WindowManager.LayoutParams lp =
  251.                 new WindowManager.LayoutParams(
  252.                         120,
  253.                         120,
  254.                         100,
  255.                         0,
  256.                         WindowManager.LayoutParams.TYPE_APPLICATION,
  257.                         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
  258.                                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
  259.                         PixelFormat.TRANSLUCENT);
  260.         // WindowManager windowManager = (WindowManager)
  261.         // this.getSystemService(Context.WINDOW_SERVICE);
  262.         windowManager.addView(overlay, lp);
  263.     }
  264.     private class LetterListViewListener implements
  265.             OnTouchingLetterChangedListener
  266.     {
  267.         @Override
  268.         public void onTouchingLetterChanged(final String s, float y, float x)
  269.         {
  270.             if (alphaIndexer.get(s) != null)
  271.             {
  272.                 int position = alphaIndexer.get(s);
  273.                 listview.setSelection(position);
  274.                 overlay.setText(sections[position]);
  275.                 overlay.setVisibility(View.VISIBLE);
  276.             }
  277.         }
  278.         @Override
  279.         public void onTouchingLetterEnd()
  280.         {
  281.             overlay.setVisibility(View.GONE);
  282.         }
  283.     }
  284.     private class OverlayThread implements Runnable
  285.     {
  286.         @Override
  287.         public void run()
  288.         {
  289.             overlay.setVisibility(View.GONE);
  290.         }
  291.     }
  292.     private String getAlpha(String str)
  293.     {
  294.         if (str == null)
  295.         {
  296.             return “#”;
  297.         }
  298.         if (str.trim().length() == 0)
  299.         {
  300.             return “#”;
  301.         }
  302.         char c = str.trim().substring(01).charAt(0);
  303.         Pattern pattern = Pattern.compile(“^[A-Za-z]+$”);
  304.         if (pattern.matcher(c + “”).matches())
  305.         {
  306.             return (c + “”).toUpperCase();
  307.         }
  308.         else
  309.         {
  310.             return “#”;
  311.         }
  312.     }
  313.     @Override
  314.     protected void onDestroy()
  315.     {
  316.         if (windowManager != null)// 防止内存泄露
  317.         {
  318.             windowManager.removeView(overlay);
  319.             windowManager.removeView(suspend_search);
  320.         }
  321.         super.onDestroy();
  322.     }
  323. }

MyLetterListView.java

  1. package com.example.menu;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Typeface;
  7. import android.util.AttributeSet;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. public class MyLetterListView extends View
  11. {
  12.     private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
  13.     private String[] b = { “@”“A”“B”“C”“D”“E”“F”“G”“H”“I”“J”“K”,
  14.             “L”“M”“N”“O”“P”“Q”“R”“S”“T”“U”“V”“W”“X”,
  15.             “Y”“Z”“#” };
  16.     int choose = –1;
  17.     private Paint paint = new Paint();
  18.     boolean showBkg = false;
  19.     public MyLetterListView(Context context, AttributeSet attrs, int defStyle)
  20.     {
  21.         super(context, attrs, defStyle);
  22.     }
  23.     public MyLetterListView(Context context, AttributeSet attrs)
  24.     {
  25.         super(context, attrs);
  26.     }
  27.     public MyLetterListView(Context context)
  28.     {
  29.         super(context);
  30.     }
  31.     protected void onDraw(Canvas canvas)
  32.     {
  33.         super.onDraw(canvas);
  34.         if (showBkg)
  35.         {
  36.             canvas.drawColor(Color.parseColor(“#40000000”));
  37.         }
  38.         int height = getHeight();
  39.         int width = getWidth();
  40.         int singleHeight = height / b.length;
  41.         for (int i = 0; i < b.length; i++)
  42.         {
  43.             paint.setTextSize(18f);
  44.             paint.setColor(Color.BLACK);
  45.             paint.setTypeface(Typeface.DEFAULT_BOLD);
  46.             paint.setAntiAlias(true);
  47.             if (i == choose)
  48.             {
  49.                 paint.setColor(Color.parseColor(“#3399ff”));
  50.                 paint.setFakeBoldText(true);
  51.             }
  52.             float xPos = width / 2 – paint.measureText(b[i]) / 2;
  53.             float yPos = singleHeight * i + singleHeight;
  54.             canvas.drawText(b[i], xPos, yPos, paint);
  55.             paint.reset();
  56.         }
  57.     }
  58.     @Override
  59.     public boolean dispatchTouchEvent(MotionEvent event)
  60.     {
  61.         final int action = event.getAction();
  62.         final float y = event.getY();
  63.         final float x = event.getX();
  64.         final int oldChoose = choose;
  65.         final OnTouchingLetterChangedListener listener =
  66.                 onTouchingLetterChangedListener;
  67.         final int c = (int) (y / getHeight() * b.length);
  68.         switch (action)
  69.         {
  70.         case MotionEvent.ACTION_DOWN:
  71.             showBkg = true;
  72.             if (oldChoose != c && listener != null)
  73.             {
  74.                 if (c > 0 && c < b.length)
  75.                 {
  76.                     listener.onTouchingLetterChanged(b[c], y, x);
  77.                     choose = c;
  78.                     invalidate();
  79.                 }
  80.             }
  81.             break;
  82.         case MotionEvent.ACTION_MOVE:
  83.             if (oldChoose != c && listener != null)
  84.             {
  85.                 if (c > 0 && c < b.length)
  86.                 {
  87.                     listener.onTouchingLetterChanged(b[c], y, x);
  88.                     choose = c;
  89.                     invalidate();
  90.                 }
  91.             }
  92.             break;
  93.         case MotionEvent.ACTION_UP:
  94.             showBkg = false;
  95.             choose = –1;
  96.             listener.onTouchingLetterEnd();
  97.             invalidate();
  98.             break;
  99.         }
  100.         return true;
  101.     }
  102.     @Override
  103.     public boolean onTouchEvent(MotionEvent event)
  104.     {
  105.         return super.onTouchEvent(event);
  106.     }
  107.     public void setOnTouchingLetterChangedListener(
  108.             OnTouchingLetterChangedListener onTouchingLetterChangedListener)
  109.     {
  110.         this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
  111.     }
  112.     public interface OnTouchingLetterChangedListener
  113.     {
  114.         public void onTouchingLetterEnd();
  115.         public void onTouchingLetterChanged(String s, float y, float x);
  116.     }
  117. }

别忘了加权限:

  1. <uses-permission android:name=“android.permission.READ_CONTACTS” />

效果图:

转自:http://blog.csdn.net/csh159/article/details/8955029

Share this:

码字很辛苦,转载请注明来自技术联盟《android仿通讯录》

评论