仿通讯录效果实现

来源:互联网 发布:查询工资的软件 编辑:程序博客网 时间:2024/04/29 05:33

代码部分抄袭

给出代码跟效果:

friend.xml

[java] view plain copy
 print?
  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.   
  11.         <ListView  
  12.             android:id="@+id/list_view"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:scrollbars="none" >  
  16.         </ListView>  
  17.   
  18.         <com.example.menu.MyLetterListView  
  19.             android:id="@+id/my_list_view"  
  20.             android:layout_width="30dip"  
  21.             android:layout_height="fill_parent"  
  22.             android:layout_alignParentRight="true"  
  23.              />  
  24.     </RelativeLayout>  
  25.   
  26. </LinearLayout>  

friend_header.xml

[html] view plain copy
 print?
  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.   
  10.     <TextView  
  11.         android:id="@+id/friend_search_head_title"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center"  
  15.         android:clickable="true"  
  16.         android:textSize="20sp" >  
  17.     </TextView>  
  18.   
  19.     <ImageView  
  20.         android:id="@+id/friend_center_back"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_gravity="right|center"  
  24.         android:clickable="true" >  
  25.     </ImageView>  
  26. </FrameLayout>  

list_item.xml

[html] view plain copy
 print?
  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.   
  6.     <TextView  
  7.         android:id="@+id/alpha"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#333333"  
  11.         android:paddingLeft="10dip"  
  12.         android:textColor="#FFFFFF"  
  13.         android:visibility="gone" />  
  14.   
  15.     <ImageView  
  16.         android:id="@+id/imageView"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:background="@drawable/contact_list_icon"  
  21.         android:layout_below="@id/alpha" />  
  22.   
  23.     <View  
  24.         android:id="@+id/divider"  
  25.         android:layout_width="1.0dip"  
  26.         android:layout_height="fill_parent"  
  27.         android:layout_below="@id/alpha"  
  28.         android:layout_marginRight="11.0dip"  
  29.         android:layout_toRightOf="@id/imageView" />  
  30.   
  31.     <TextView  
  32.         android:id="@+id/name"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_alignTop="@id/divider"  
  36.         android:layout_marginLeft="2.0dip"  
  37.         android:layout_marginRight="5.0dip"  
  38.         android:layout_marginTop="6.0dip"  
  39.         android:layout_toRightOf="@id/divider"  
  40.         android:singleLine="true"  
  41.         android:textAppearance="?android:textAppearanceMedium" />  
  42.   
  43.     <TextView  
  44.         android:id="@+id/number"  
  45.         android:layout_width="wrap_content"  
  46.         android:layout_height="wrap_content"  
  47.         android:layout_alignLeft="@id/name"  
  48.         android:layout_alignWithParentIfMissing="true"  
  49.         android:layout_below="@id/name"  
  50.         android:ellipsize="marquee"  
  51.         android:singleLine="true"  
  52.         android:textAppearance="?android:textAppearanceSmall" />  
  53.   
  54. </RelativeLayout>  

overlay.xml

[html] view plain copy
 print?
  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.       
  7.   
  8. </TextView>  
suspend_search.xml

[html] view plain copy
 print?
  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.   
  7. </ImageView>  

Friend.java

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

MyLetterListView.java

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

别忘了加权限:
[html] view plain copy
 print?
  1. <uses-permission android:name="android.permission.READ_CONTACTS" />  

效果图:







0 0
原创粉丝点击