zoukankan      html  css  js  c++  java
  • Android系统联系人全特效实现(下),字母表快速滚动

    在上一篇文章中,我和大家一起实现了类似于Android系统联系人的分组导航和挤压动画功能,不过既然文章名叫做《Android系统联系人全特效实现》,那么没有快速滚动功能显然是称不上"全"的。因此本篇文章我将带领大家在上篇文章的代码基础上改进,加入快速滚动功能。

    如果还没有看过我上一篇文章,请抓紧去阅读一下 Android系统联系人全特效实现(上),分组导航和挤压动画 。

    其实ListView本身是有一个快速滚动属性的,可以通过在XML中设置android:fastScrollEnabled="true"来启用。包括以前老版本的Android联系人中都是使用这种方式来进行快速滚动的。效果如下图所示:

                                             

    不过这种快速滚动方式比较丑陋,到后来很多手机厂商在定制自己ROM的时候都将默认快速滚动改成了类似iPhone上A-Z字母表快速滚动的方式。这里我们怎么能落后于时代的潮流呢!我们的快速滚动也要使用A-Z字母表的方式!

    下面就来开始实现,首先打开上次的ContactsDemo工程,修改activity_main.xml布局文件。由于我们要在界面上加入字母表,因此我们需要一个Button,将这个Button的背景设为一张A-Z排序的图片,然后居右对齐。另外还需要一个TextView,用于在弹出式分组布局上显示当前的分组,默认是gone掉的,只有手指在字母表上滑动时才让它显示出来。修改后的布局文件代码如下:

    [html] view plaincopy
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <ListView  
    8.         android:id="@+id/contacts_list_view"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="wrap_content"  
    11.         android:layout_alignParentTop="true"  
    12.         android:scrollbars="none"  
    13.         android:fadingEdge="none" >  
    14.     </ListView>  
    15.   
    16.     <LinearLayout  
    17.         android:id="@+id/title_layout"  
    18.         android:layout_width="fill_parent"  
    19.         android:layout_height="18dip"  
    20.         android:layout_alignParentTop="true"  
    21.         android:background="#303030" >  
    22.   
    23.         <TextView  
    24.             android:id="@+id/title"  
    25.             android:layout_width="wrap_content"  
    26.             android:layout_height="wrap_content"  
    27.             android:layout_gravity="center_horizontal"  
    28.             android:layout_marginLeft="10dip"  
    29.             android:textColor="#ffffff"  
    30.             android:textSize="13sp" />  
    31.     </LinearLayout>  
    32.       
    33.     <Button   
    34.         android:id="@+id/alphabetButton"  
    35.         android:layout_width="wrap_content"  
    36.         android:layout_height="fill_parent"  
    37.         android:layout_alignParentRight="true"  
    38.         android:background="@drawable/a_z"  
    39.         />  
    40.       
    41.     <RelativeLayout   
    42.         android:id="@+id/section_toast_layout"  
    43.         android:layout_width="70dip"  
    44.         android:layout_height="70dip"  
    45.         android:layout_centerInParent="true"  
    46.         android:background="@drawable/section_toast"  
    47.         android:visibility="gone"  
    48.         >  
    49.         <TextView   
    50.             android:id="@+id/section_toast_text"  
    51.             android:layout_width="wrap_content"  
    52.             android:layout_height="wrap_content"  
    53.             android:layout_centerInParent="true"  
    54.             android:textColor="#fff"  
    55.             android:textSize="30sp"  
    56.             />  
    57.     </RelativeLayout>  
    58.   
    59. </RelativeLayout>  
    然后打开MainActivity进行修改,毫无疑问,我们需要对字母表按钮的touch事件进行监听,于是在MainActivity中新增如下代码:
    [java] view plaincopy
    1. private void setAlpabetListener() {  
    2.     alphabetButton.setOnTouchListener(new OnTouchListener() {  
    3.         @Override  
    4.         public boolean onTouch(View v, MotionEvent event) {  
    5.             float alphabetHeight = alphabetButton.getHeight();  
    6.             float y = event.getY();  
    7.             int sectionPosition = (int) ((y / alphabetHeight) / (1f / 27f));  
    8.             if (sectionPosition < 0) {  
    9.                 sectionPosition = 0;  
    10.             } else if (sectionPosition > 26) {  
    11.                 sectionPosition = 26;  
    12.             }  
    13.             String sectionLetter = String.valueOf(alphabet.charAt(sectionPosition));  
    14.             int position = indexer.getPositionForSection(sectionPosition);  
    15.             switch (event.getAction()) {  
    16.             case MotionEvent.ACTION_DOWN:  
    17.                 alphabetButton.setBackgroundResource(R.drawable.a_z_click);  
    18.                 sectionToastLayout.setVisibility(View.VISIBLE);  
    19.                 sectionToastText.setText(sectionLetter);  
    20.                 contactsListView.setSelection(position);  
    21.                 break;  
    22.             case MotionEvent.ACTION_MOVE:  
    23.                 sectionToastText.setText(sectionLetter);  
    24.                 contactsListView.setSelection(position);  
    25.                 break;  
    26.             default:  
    27.                 alphabetButton.setBackgroundResource(R.drawable.a_z);  
    28.                 sectionToastLayout.setVisibility(View.GONE);  
    29.             }  
    30.             return true;  
    31.         }  
    32.     });  
    33. }  
    可以看到,在这个方法中我们注册了字母表按钮的onTouch事件,然后在onTouch方法里做了一些逻辑判断和处理,下面我来一一详细说明。首先通过字母表按钮的getHeight方法获取到字母表的总高度,然后用event.getY方法获取到目前手指在字母表上的纵坐标,用纵坐标除以总高度就可以得到一个用小数表示的当前手指所在位置(0表在#端,1表示在Z端)。由于我们的字母表中一共有27个字符,再用刚刚算出的小数再除以1/27就可以得到一个0到27范围内的浮点数,之后再把这个浮点数向下取整,就可以算出我们当前按在哪个字母上了。然后再对event的action进行判断,如果是ACTION_DOWN或ACTION_MOVE,就在弹出式分组上显示当前手指所按的字母,并调用ListView的setSelection方法把列表滚动到相应的分组。如果是其它的action,就将弹出式分组布局隐藏。
    MainActivity的完整代码如下:
    [java] view plaincopy
    1. public class MainActivity extends Activity {  
    2.   
    3.     /** 
    4.      * 分组的布局 
    5.      */  
    6.     private LinearLayout titleLayout;  
    7.   
    8.     /** 
    9.      * 弹出式分组的布局 
    10.      */  
    11.     private RelativeLayout sectionToastLayout;  
    12.   
    13.     /** 
    14.      * 右侧可滑动字母表 
    15.      */  
    16.     private Button alphabetButton;  
    17.   
    18.     /** 
    19.      * 分组上显示的字母 
    20.      */  
    21.     private TextView title;  
    22.   
    23.     /** 
    24.      * 弹出式分组上的文字 
    25.      */  
    26.     private TextView sectionToastText;  
    27.   
    28.     /** 
    29.      * 联系人ListView 
    30.      */  
    31.     private ListView contactsListView;  
    32.   
    33.     /** 
    34.      * 联系人列表适配器 
    35.      */  
    36.     private ContactAdapter adapter;  
    37.   
    38.     /** 
    39.      * 用于进行字母表分组 
    40.      */  
    41.     private AlphabetIndexer indexer;  
    42.   
    43.     /** 
    44.      * 存储所有手机中的联系人 
    45.      */  
    46.     private List<Contact> contacts = new ArrayList<Contact>();  
    47.   
    48.     /** 
    49.      * 定义字母表的排序规则 
    50.      */  
    51.     private String alphabet = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    52.   
    53.     /** 
    54.      * 上次第一个可见元素,用于滚动时记录标识。 
    55.      */  
    56.     private int lastFirstVisibleItem = -1;  
    57.   
    58.     @Override  
    59.     protected void onCreate(Bundle savedInstanceState) {  
    60.         super.onCreate(savedInstanceState);  
    61.         setContentView(R.layout.activity_main);  
    62.         adapter = new ContactAdapter(this, R.layout.contact_item, contacts);  
    63.         titleLayout = (LinearLayout) findViewById(R.id.title_layout);  
    64.         sectionToastLayout = (RelativeLayout) findViewById(R.id.section_toast_layout);  
    65.         title = (TextView) findViewById(R.id.title);  
    66.         sectionToastText = (TextView) findViewById(R.id.section_toast_text);  
    67.         alphabetButton = (Button) findViewById(R.id.alphabetButton);  
    68.         contactsListView = (ListView) findViewById(R.id.contacts_list_view);  
    69.         Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;  
    70.         Cursor cursor = getContentResolver().query(uri,  
    71.                 new String[] { "display_name""sort_key" }, nullnull"sort_key");  
    72.         if (cursor.moveToFirst()) {  
    73.             do {  
    74.                 String name = cursor.getString(0);  
    75.                 String sortKey = getSortKey(cursor.getString(1));  
    76.                 Contact contact = new Contact();  
    77.                 contact.setName(name);  
    78.                 contact.setSortKey(sortKey);  
    79.                 contacts.add(contact);  
    80.             } while (cursor.moveToNext());  
    81.         }  
    82.         startManagingCursor(cursor);  
    83.         indexer = new AlphabetIndexer(cursor, 1, alphabet);  
    84.         adapter.setIndexer(indexer);  
    85.         if (contacts.size() > 0) {  
    86.             setupContactsListView();  
    87.             setAlpabetListener();  
    88.         }  
    89.     }  
    90.   
    91.     /** 
    92.      * 为联系人ListView设置监听事件,根据当前的滑动状态来改变分组的显示位置,从而实现挤压动画的效果。 
    93.      */  
    94.     private void setupContactsListView() {  
    95.         contactsListView.setAdapter(adapter);  
    96.         contactsListView.setOnScrollListener(new OnScrollListener() {  
    97.             @Override  
    98.             public void onScrollStateChanged(AbsListView view, int scrollState) {  
    99.             }  
    100.   
    101.             @Override  
    102.             public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,  
    103.                     int totalItemCount) {  
    104.                 int section = indexer.getSectionForPosition(firstVisibleItem);  
    105.                 int nextSecPosition = indexer.getPositionForSection(section + 1);  
    106.                 if (firstVisibleItem != lastFirstVisibleItem) {  
    107.                     MarginLayoutParams params = (MarginLayoutParams) titleLayout.getLayoutParams();  
    108.                     params.topMargin = 0;  
    109.                     titleLayout.setLayoutParams(params);  
    110.                     title.setText(String.valueOf(alphabet.charAt(section)));  
    111.                 }  
    112.                 if (nextSecPosition == firstVisibleItem + 1) {  
    113.                     View childView = view.getChildAt(0);  
    114.                     if (childView != null) {  
    115.                         int titleHeight = titleLayout.getHeight();  
    116.                         int bottom = childView.getBottom();  
    117.                         MarginLayoutParams params = (MarginLayoutParams) titleLayout  
    118.                                 .getLayoutParams();  
    119.                         if (bottom < titleHeight) {  
    120.                             float pushedDistance = bottom - titleHeight;  
    121.                             params.topMargin = (int) pushedDistance;  
    122.                             titleLayout.setLayoutParams(params);  
    123.                         } else {  
    124.                             if (params.topMargin != 0) {  
    125.                                 params.topMargin = 0;  
    126.                                 titleLayout.setLayoutParams(params);  
    127.                             }  
    128.                         }  
    129.                     }  
    130.                 }  
    131.                 lastFirstVisibleItem = firstVisibleItem;  
    132.             }  
    133.         });  
    134.   
    135.     }  
    136.   
    137.     /** 
    138.      * 设置字母表上的触摸事件,根据当前触摸的位置结合字母表的高度,计算出当前触摸在哪个字母上。 
    139.      * 当手指按在字母表上时,展示弹出式分组。手指离开字母表时,将弹出式分组隐藏。 
    140.      */  
    141.     private void setAlpabetListener() {  
    142.         alphabetButton.setOnTouchListener(new OnTouchListener() {  
    143.             @Override  
    144.             public boolean onTouch(View v, MotionEvent event) {  
    145.                 float alphabetHeight = alphabetButton.getHeight();  
    146.                 float y = event.getY();  
    147.                 int sectionPosition = (int) ((y / alphabetHeight) / (1f / 27f));  
    148.                 if (sectionPosition < 0) {  
    149.                     sectionPosition = 0;  
    150.                 } else if (sectionPosition > 26) {  
    151.                     sectionPosition = 26;  
    152.                 }  
    153.                 String sectionLetter = String.valueOf(alphabet.charAt(sectionPosition));  
    154.                 int position = indexer.getPositionForSection(sectionPosition);  
    155.                 switch (event.getAction()) {  
    156.                 case MotionEvent.ACTION_DOWN:  
    157.                     alphabetButton.setBackgroundResource(R.drawable.a_z_click);  
    158.                     sectionToastLayout.setVisibility(View.VISIBLE);  
    159.                     sectionToastText.setText(sectionLetter);  
    160.                     contactsListView.setSelection(position);  
    161.                     break;  
    162.                 case MotionEvent.ACTION_MOVE:  
    163.                     sectionToastText.setText(sectionLetter);  
    164.                     contactsListView.setSelection(position);  
    165.                     break;  
    166.                 default:  
    167.                     alphabetButton.setBackgroundResource(R.drawable.a_z);  
    168.                     sectionToastLayout.setVisibility(View.GONE);  
    169.                 }  
    170.                 return true;  
    171.             }  
    172.         });  
    173.     }  
    174.   
    175.     /** 
    176.      * 获取sort key的首个字符,如果是英文字母就直接返回,否则返回#。 
    177.      *  
    178.      * @param sortKeyString 
    179.      *            数据库中读取出的sort key 
    180.      * @return 英文字母或者# 
    181.      */  
    182.     private String getSortKey(String sortKeyString) {  
    183.         alphabetButton.getHeight();  
    184.         String key = sortKeyString.substring(01).toUpperCase();  
    185.         if (key.matches("[A-Z]")) {  
    186.             return key;  
    187.         }  
    188.         return "#";  
    189.     }  
    190.   
    191. }  
    好了,就改动了以上两处,其它文件都保持不变,让我们来运行一下看看效果:

                                       

    非常不错!当你的手指在右侧字母表上滑动时,联系人的列表也跟着相应的变动,并在屏幕中央显示一个当前的分组。

    现在让我们回数一下,分组导航、挤压动画、字母表快速滚动,Android系统联系人全特效都实现了!

    好了,今天的讲解到此结束,有疑问的朋友请在下面留言。

    源码下载,请点击这里

  • 相关阅读:
    计算机图形学和OpenGL(二)坐标系和绘制点线函数
    计算机图形学和OpenGL(一)OpenGL初步
    C++ 实现链表常用功能
    Cocos2d-x环境搭建
    2014年学习计划
    2013年终总结
    AS3开发必须掌握的内容
    starling性能优化
    后补个2012年的总结吧
    原生javascript实现图片懒加载
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470028.html
Copyright © 2011-2022 走看看