现在的位置: 首页 > 综合 > 正文

android联系人源码解析

2018年04月21日 ⁄ 综合 ⁄ 共 4769字 ⁄ 字号 评论关闭

转载请注明出处:http://blog.csdn.net/droyon/article/details/9102797

源代码请从官方网站下载,本文针对源代码增加上自己的理解。

[java] view
plain
copy

  1. package com.android.contacts.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.ListView;  
  6.   
  7. /** 
  8.  * A ListView that can be asked to scroll (smoothly or otherwise) to a specific 
  9.  * position.  This class takes advantage of similar functionality that exists 
  10.  * in {@link ListView} and enhances it. 
  11.  * <strong><span style="color:#ff0000;">联系人应用中自定义组件,增强了ListView的功能。 
  12.  * 用于滑动到某一个位置 以及 在到达某个位置时是否平滑</span></strong> 
  13.  */  
  14. public class AutoScrollListView extends ListView {  
  15.   
  16.     /** 
  17.      * Position the element at about 1/3 of the list height 
  18.      */  
  19.     private static final float PREFERRED_SELECTION_OFFSET_FROM_TOP = 0.33f;  
  20.   
  21.     private int mRequestedScrollPosition = -1;  
  22.     private boolean mSmoothScrollRequested;  
  23.   
  24.     public AutoScrollListView(Context context) {  
  25.         super(context);  
  26.     }  
  27.   
  28.     public AutoScrollListView(Context context, AttributeSet attrs) {  
  29.         super(context, attrs);  
  30.     }  
  31.   
  32.     public AutoScrollListView(Context context, AttributeSet attrs, int defStyle) {  
  33.         super(context, attrs, defStyle);  
  34.     }  
  35.   
  36.     /** 
  37.      * Brings the specified position to view by optionally performing a jump-scroll maneuver: 
  38.      * first it jumps to some position near the one requested and then does a smooth 
  39.      * scroll to the requested position.  This creates an impression of full smooth 
  40.      * scrolling without actually traversing the entire list.  If smooth scrolling is 
  41.      * not requested, instantly positions the requested item at a preferred offset. 
  42.      * <strong><span style="color:#006600;">提供的一个方法,是否平滑滑动到position处</span></strong> 
  43.      */  
  44.     public void requestPositionToScreen(int position, boolean smoothScroll) {  
  45.         mRequestedScrollPosition = position;  
  46.         mSmoothScrollRequested = smoothScroll;  
  47.         requestLayout();  
  48.     }  
  49.    <strong><span style="color:#006600;"/** 
  50.      * 这个方法在ListView(ListView包含了一系列的Item【项】)中用于布局ListViewItem。 
  51.      */</span></strong>  
  52.     @Override  
  53.     protected void layoutChildren() {<strong><span style="color:#006600;">//重写的ListView中的方法,这个方法,主要就是封装了ListView中的layoutChildren。</span></strong>  
  54.         super.layoutChildren();  
  55.         if (mRequestedScrollPosition == -1) {  
  56.             return;  
  57.         }  
  58.   
  59.         final int position = mRequestedScrollPosition;  
  60.         mRequestedScrollPosition = -1;  
  61.   
  62.         int firstPosition = getFirstVisiblePosition() + 1;  
  63.         int lastPosition = getLastVisiblePosition();  
  64.         if (position >= firstPosition && position <= lastPosition) {  
  65.             return// Already on screen  
  66.         }  
  67.   
  68.         final int offset = (int) (getHeight() * PREFERRED_SELECTION_OFFSET_FROM_TOP);  
  69.         if (!mSmoothScrollRequested) {  
  70.             setSelectionFromTop(position, offset);  
  71.   
  72.             // Since we have changed the scrolling position, we need to redo child layout  
  73.             // Calling "requestLayout" in the middle of a layout pass has no effect,  
  74.             // so we call layoutChildren explicitly  
  75.             <strong><span style="color:#006600;">//如果不需要平滑的滑动到position处,直接调用父类的layoutChildren方法。</span></strong>  
  76.             super.layoutChildren();  
  77.   
  78.         } else {  
  79.             // We will first position the list a couple of screens before or after  
  80.             // the new selection and then scroll smoothly to it.  
  81.             <strong><span style="color:#006600;">//现在是处理平滑滑动到某个位置了,如何处理那?  
  82.             //我们首先需要将当前的list条目焦点设定在离要滑动的位置2屏内。为什么这么处理那?  
  83.             //设想一下,list有1000个条目,一屏可以存放10条条目,我们当前处在最后一屏(也就是当前显示990~1000),那么我们要平滑滑动的位置为1  
  84.             //我们的listView要从990一直平滑滑动到1,这要多费功夫,  
  85.             //所以,直接把焦点设定在list的20处(2屏数目),然后从20位置平滑过度到1。</span></strong>  
  86.             int twoScreens = (lastPosition - firstPosition) * 2;  
  87.             int preliminaryPosition;  
  88.             if (position < firstPosition) {  
  89.                 preliminaryPosition = position + twoScreens;  
  90.                 if (preliminaryPosition >= getCount()) {  
  91.                     preliminaryPosition = getCount() - 1;  
  92.                 }  
  93.                 if (preliminaryPosition < firstPosition) {  
  94.                     setSelection(preliminaryPosition);  
  95.                     super.layoutChildren();  
  96.                 }  
  97.             } else {  
  98.                 preliminaryPosition = position - twoScreens;  
  99.                 if (preliminaryPosition < 0) {  
  100.                     preliminaryPosition = 0;  
  101.                 }  
  102.                 if (preliminaryPosition > lastPosition) {  
  103.                     setSelection(preliminaryPosition);  
  104.                     super.layoutChildren();  
  105.                 }  
  106.             }  
  107.   
  108.   
  109.             smoothScrollToPositionFromTop(position, offset);  
  110.         }  
  111.     }  
  112. }  

抱歉!评论已关闭.