自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

想要親手實(shí)現(xiàn)一個(gè)刷新控件,你只需要掌握這些知識(shí)

移動(dòng)開發(fā) Android
現(xiàn)在Android陣營(yíng)里面的刷新控件很多,稂莠不齊。筆者試圖從不一樣的角度,在它的個(gè)性化和滾動(dòng)上下一些功夫。筆者期望,這個(gè)刷新控件能像Google的SwipeRefreshLayout一樣,支持大多數(shù)列表控件,有加載更多功能,最好是要很方便的支持個(gè)性化,滾動(dòng)中能夠越界是不是也會(huì)帶來比普通的刷新控件更好的交互體驗(yàn)。

現(xiàn)在Android陣營(yíng)里面的刷新控件很多,稂莠不齊。筆者試圖從不一樣的角度,在它的個(gè)性化和滾動(dòng)上下一些功夫。筆者期望,這個(gè)刷新控件能像Google的SwipeRefreshLayout一樣,支持大多數(shù)列表控件,有加載更多功能,最好是要很方便的支持個(gè)性化,滾動(dòng)中能夠越界是不是也會(huì)帶來比普通的刷新控件更好的交互體驗(yàn)。開源庫在這,TwinklingRefreshLayout,如果喜歡請(qǐng)star,筆者的文章也是圍繞著這個(gè)控件的實(shí)現(xiàn)來說的。

為了方便,筆者將TwinklingRefreshLayout直接繼承自FrameLayout而不是ViewGroup,可以省去onMeasure、onLayout等一些麻煩,Header和Footer則是通過LayoutParams來設(shè)置View的Gravity屬性來做的。

1. View的onAttachedToWindow()方法

首先View沒有明顯的生命周期,我們又不能再構(gòu)造函數(shù)里面addView()給控件添加頭部和底部,因此這個(gè)操作比較合適的時(shí)機(jī)就是在onDraw()之前——onAttachedToWindow()方法中。

此時(shí)View被添加到了窗體上,View有了一個(gè)用于顯示的Surface,將開始繪制。因此其保證了在onDraw()之前調(diào)用,但可能在調(diào)用 onDraw(Canvas) 之前的任何時(shí)刻,包括調(diào)用 onMeasure(int, int) 之前或之后。比較適合去執(zhí)行一些初始化操作。(此外在屏蔽Home鍵的時(shí)候也會(huì)回調(diào)這個(gè)方法)

  • onDetachedFromWindow()與onAttachedToWindow()方法相對(duì)應(yīng)。
  • ViewGroup先是調(diào)用自己的onAttachedToWindow()方法,再調(diào)用其每個(gè)child的onAttachedToWindow()方法,這樣此方法就在整個(gè)view樹中遍布開了,而visibility并不會(huì)對(duì)這個(gè)方法產(chǎn)生影響。
  • onAttachedToWindow方法是在Activity resume的時(shí)候被調(diào)用的,也就是act對(duì)應(yīng)的window被添加的時(shí)候,且每個(gè)view只會(huì)被調(diào)用一次,父view的調(diào)用在前,不論view的visibility狀態(tài)都會(huì)被調(diào)用,適合做些view特定的初始化操作;
  • onDetachedFromWindow方法是在Activity destroy的時(shí)候被調(diào)用的,也就是act對(duì)應(yīng)的window被刪除的時(shí)候,且每個(gè)view只會(huì)被調(diào)用一次,父view的調(diào)用在后,也不論view的visibility狀態(tài)都會(huì)被調(diào)用,適合做最后的清理操作;

就TwinklingRefreshLayout來說,Header和Footer需要及時(shí)顯示出來,View又沒有明顯的生命周期,因此在onAttachedToWindow()中進(jìn)行設(shè)置可以保證在onDraw()之前添加了刷新控件。

  1. @Override 
  2.     protected void onAttachedToWindow() { 
  3.         super.onAttachedToWindow(); 
  4.  
  5.         //添加頭部 
  6.         FrameLayout headViewLayout = new FrameLayout(getContext()); 
  7.         LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0); 
  8.         layoutParams.gravity = Gravity.TOP
  9.         headViewLayout.setLayoutParams(layoutParams); 
  10.  
  11.         mHeadLayout = headViewLayout; 
  12.         this.addView(mHeadLayout);//addView(view,-1)添加到-1的位置 
  13.  
  14.         //添加底部 
  15.         FrameLayout bottomViewLayout = new FrameLayout(getContext()); 
  16.         LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0); 
  17.         layoutParams2.gravity = Gravity.BOTTOM; 
  18.         bottomViewLayout.setLayoutParams(layoutParams2); 
  19.  
  20.         mBottomLayout = bottomViewLayout; 
  21.         this.addView(mBottomLayout); 
  22.         //...其它步驟 
  23.     }  

但是當(dāng)TwinklingRefreshLayout應(yīng)用在Activity或Fragment中時(shí),可能會(huì)因?yàn)閳?zhí)行onResume重新觸發(fā)了onAttachedToWindow()方法而導(dǎo)致重復(fù)創(chuàng)建Header和Footer擋住原先添加的View,因此需要加上判斷:

  1. @Override 
  2.    protected void onAttachedToWindow() { 
  3.        super.onAttachedToWindow(); 
  4.        System.out.println("onAttachedToWindow綁定窗口"); 
  5.  
  6.        //添加頭部 
  7.        if (mHeadLayout == null) { 
  8.            FrameLayout headViewLayout = new FrameLayout(getContext()); 
  9.            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0); 
  10.            layoutParams.gravity = Gravity.TOP
  11.            headViewLayout.setLayoutParams(layoutParams); 
  12.  
  13.            mHeadLayout = headViewLayout; 
  14.  
  15.            this.addView(mHeadLayout);//addView(view,-1)添加到-1的位置 
  16.  
  17.            if (mHeadView == null) setHeaderView(new RoundDotView(getContext())); 
  18.        } 
  19.        //... 
  20.    }  

2. View的事件分發(fā)機(jī)制

事件的分發(fā)過程由dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent三個(gè)方法來共同完成的。由于事件的傳遞是自頂向下的,對(duì)于ViewGroup,筆者覺得最重要的就是onInterceptTouchEvent方法了,它關(guān)系到事件是否能夠繼續(xù)向下傳遞??慈缦聜未a:

  1. public boolean dispatchTouchEvent(MotionEvenet ev){ 
  2.     boolean consume = false
  3.     if (onInterceptTouchEvent(ev)) { 
  4.         consume = onTouchEvent(ev); 
  5.     }else
  6.         consume = child.dispatchTouchEvent(ev); 
  7.     } 
  8.     return consume; 
  9.  

如代碼所示,如果ViewGroup攔截了(onInterceptTouchEvent返回true)事件,則事件會(huì)在ViewGroup的onTouchEvent方法中消費(fèi),而不會(huì)傳到子View;否則事件將交給子View去分發(fā)。

我們需要做的就是在子View滾動(dòng)到頂部或者底部時(shí)及時(shí)的攔截事件,讓ViewGroup的onTouchEvent來交接處理滑動(dòng)事件。

3. 判斷子View滾動(dòng)達(dá)到邊界

在什么時(shí)候?qū)κ录M(jìn)行攔截呢?對(duì)于Header,當(dāng)手指向下滑動(dòng)也就是 dy>0 且子View已經(jīng)滾動(dòng)到頂部(不能再向上滾動(dòng))時(shí)攔截;對(duì)于bottom則是 dy<0 且子View已經(jīng)滾動(dòng)到底部(不能再向下滾動(dòng))時(shí)攔截:

  1. @Override 
  2.     public boolean onInterceptTouchEvent(MotionEvent ev) { 
  3.         switch (ev.getAction()) { 
  4.             case MotionEvent.ACTION_DOWN: 
  5.                 mTouchY = ev.getY(); 
  6.                 break; 
  7.             case MotionEvent.ACTION_MOVE: 
  8.                 float dy = ev.getY() - mTouchY; 
  9.  
  10.                 if (dy > 0 && !canChildScrollUp()) { 
  11.                     state = PULL_DOWN_REFRESH; 
  12.                     return true
  13.                 } else if (dy < 0 && !canChildScrollDown() && enableLoadmore) { 
  14.                     state = PULL_UP_LOAD; 
  15.                     return true
  16.                 } 
  17.                 break; 
  18.         } 
  19.         return super.onInterceptTouchEvent(ev); 
  20.     }  

判斷View能不能繼續(xù)向上滾動(dòng),對(duì)于sdk14以上版本,v4包里提供了方法:

  1. public boolean canChildScrollUp() { 
  2.     return ViewCompat.canScrollVertically(mChildView, -1); 
  3.  

其它情況,直接交給子View了,ViewGroup這里也管不著。

4. ViewGroup 的 onTouchEvent 方法

走到這一步,子View的滾動(dòng)已經(jīng)交給子View自己去搞了,ViewGroup需要處理的事件只有兩個(gè)臨界狀態(tài),也就是用戶在下拉可能想要刷新的狀態(tài)和用戶在上拉可能想要加載更多的狀態(tài)。也就是上面state記錄的狀態(tài)。接下來的事情就簡(jiǎn)單咯,監(jiān)聽一下ACTION_MOVE和ACTION_UP就好了。

首先在ACTION_DOWN時(shí)需要記錄下最原先的手指按下的位置 mTouchY,然后在一系列ACTION_MOVE過程中,獲取當(dāng)前位移(ev.getY()-mTouchY),然后通過 某種計(jì)算方式 不斷計(jì)算當(dāng)前的子View應(yīng)該位移的距離offsetY,調(diào)用mChildView.setTranslationY(offsetY)來不斷設(shè)置子View的位移,同時(shí)需要給HeadLayout申請(qǐng)布局高度來完成頂部控件的顯示。這其中筆者使用的計(jì)算方式就是插值器(Interpolator)。

在ACTION_UP時(shí),需要判斷子View的位移有沒有達(dá)到進(jìn)入刷新或者是加載更多狀態(tài)的要求,即mChildView.getTranslationY() >= mHeadHeight - mTouchSlop,mTouchSlop是為了防止發(fā)生抖動(dòng)而存在。判斷進(jìn)入了刷新狀態(tài)時(shí),當(dāng)前子View的位移在HeadHeight和maxHeadHeight之間,所以需要讓子View的位移回到HeadHeight處,否則就直接回到0處。

5. Interpolator插值器

Interpolator用于動(dòng)畫中的時(shí)間插值,其作用就是把0到1的浮點(diǎn)值變化映射到另一個(gè)浮點(diǎn)值變化。上面提到的計(jì)算方式如下:

  1. float offsetY = decelerateInterpolator.getInterpolation(dy / mWaveHeight / 2) * dy / 2; 

其中(dy / mWaveHeight / 2)是一個(gè)0~1之間的浮點(diǎn)值,隨著下拉高度的增加,這個(gè)值越來越大,通過decelerateInterpolator獲取到的插值也越來越大,只不過這些值的變化量是越來越小(decelerate效果)。dy表示的是手指移動(dòng)的距離。這只是筆者為了滑動(dòng)的柔和性使用的一種計(jì)算方式,頭部位移的最大距離是mWaveHeight = dy/2,這樣看的話可以發(fā)現(xiàn) dy / mWaveHeight / 2 會(huì)從0到1變化。Interpolator繼承自TimeInterpolator接口,源碼如下:

  1. public interface TimeInterpolator { 
  2.     /** 
  3.      * Maps a value representing the elapsed fraction of an animation to a value that represents 
  4.      * the interpolated fraction. This interpolated value is then multiplied by the change in 
  5.      * value of an animation to derive the animated value at the current elapsed animation time
  6.      * 
  7.      * @param input A value between 0 and 1.0 indicating our current point 
  8.      *        in the animation where 0 represents the start and 1.0 represents 
  9.      *        the end 
  10.      * @return The interpolation value. This value can be more than 1.0 for 
  11.      *         interpolators which overshoot their targets, or less than 0 for 
  12.      *         interpolators that undershoot their targets. 
  13.      */ 
  14.     float getInterpolation(float input); 
  15.  

getInterpolation接收一個(gè)0.0~1.0之間的float參數(shù),0.0代表動(dòng)畫的開始,1.0代表動(dòng)畫的結(jié)束。返回值則可以超過1.0,也可以小于0.0,比如OvershotInterpolator。所以getInterpolation()是用來實(shí)現(xiàn)輸入0~1返回0~1左右的函數(shù)值的一個(gè)函數(shù)。  

 

6. 屬性動(dòng)畫

上面說到了手指抬起的時(shí)候,mChildView的位移要么回到mHeadHeight處,要么回到0處。直接setTranslationY()不免太不友好,所以我們這里使用屬性動(dòng)畫來做。本來是直接可以用mChildView.animate()方法來完成屬性動(dòng)畫的,因?yàn)樾枰嫒莸桶姹静⒒卣{(diào)一些參數(shù),所以這里使用ObjectAnimator:

  1. private void animChildView(float endValue, long duration) { 
  2.         ObjectAnimator oa = ObjectAnimator.ofFloat(mChildView, "translationY", mChildView.getTranslationY(), endValue); 
  3.         oa.setDuration(duration); 
  4.         oa.setInterpolator(new DecelerateInterpolator());//設(shè)置速率為遞減 
  5.         oa.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
  6.             @Override 
  7.             public void onAnimationUpdate(ValueAnimator animation) { 
  8.                 int height = (int) mChildView.getTranslationY();//獲得mChildView當(dāng)前y的位置 
  9.                 height = Math.abs(height); 
  10.  
  11.                 mHeadLayout.getLayoutParams().height = height; 
  12.                 mHeadLayout.requestLayout(); 
  13.             } 
  14.         }); 
  15.     oa.start(); 
  16.  

傳統(tǒng)的補(bǔ)間動(dòng)畫只能夠?qū)崿F(xiàn)移動(dòng)、縮放、旋轉(zhuǎn)和淡入淡出這四種動(dòng)畫操作,而且它只是改變了View的顯示效果,改變了畫布繪制出來的樣子,而不會(huì)真正去改變View的屬性。比如用補(bǔ)間動(dòng)畫對(duì)一個(gè)按鈕進(jìn)行了移動(dòng),只有在原位置點(diǎn)擊按鈕才會(huì)發(fā)生響應(yīng),而屬性動(dòng)畫則可以真正的移動(dòng)按鈕。屬性動(dòng)畫最簡(jiǎn)單的一種使用方式就是使用ValueAnimator:

  1. ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);   
  2. anim.start();  

它可以傳入多個(gè)參數(shù),如ValueAnimator.ofFloat(0f, 5f, 3f, 10f),他會(huì)根據(jù)設(shè)置的插值器依次計(jì)算,比如想做一個(gè)心跳的效果,用ValueAnimator來控制心的當(dāng)前縮放值大小就是個(gè)不錯(cuò)的選擇。除此之外,還可以調(diào)用setStartDelay()方法來設(shè)置動(dòng)畫延遲播放的時(shí)間,調(diào)用setRepeatCount()和setRepeatMode()方法來設(shè)置動(dòng)畫循環(huán)播放的次數(shù)以及循環(huán)播放的模式等。

如果想要實(shí)現(xiàn)View的位移,ValueAnimator顯然是比較麻煩的,我們可以使用ValueAnimator的子類ObjectAnimator,如下:

  1. ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);   
  2. animator.setDuration(5000);   
  3. animator.start();    

傳入的第一個(gè)值是Object,不局限于View,傳入的第二個(gè)參數(shù)為Object的一個(gè)屬性,比如傳入"abc",ObjectAnimator會(huì)去Object里面找有沒有 getAbc() 和 setAbc(...) 這兩個(gè)方法,如果沒有,動(dòng)畫就沒有效果,它內(nèi)部應(yīng)該是處理了相應(yīng)的異常。另外還可以用AnimatorSet來實(shí)現(xiàn)多個(gè)屬性動(dòng)畫同時(shí)播放,也可以在xml中寫屬性動(dòng)畫。

7. 個(gè)性化Header和Footer的接口

要實(shí)現(xiàn)個(gè)性化的Header和Footer,最最重要的當(dāng)然是把滑動(dòng)過程中系數(shù)都回調(diào)出來啦。在ACTION_MOVE的時(shí)候,在ACTION_UP的時(shí)候,還有在mChildView在執(zhí)行屬性動(dòng)畫的時(shí)候,而且mChildView當(dāng)前所處的狀態(tài)都是很明確的,寫個(gè)接口就好了。

  1. public interface IHeaderView { 
  2.     View getView(); 
  3.  
  4.     void onPullingDown(float fraction,float maxHeadHeight,float headHeight); 
  5.  
  6.     void onPullReleasing(float fraction,float maxHeadHeight,float headHeight); 
  7.  
  8.     void startAnim(float maxHeadHeight,float headHeight); 
  9.  
  10.     void onFinish(); 
  11.  

getView()方法保證在TwinklingRefreshLayout中可以取到在外部設(shè)置的View,onPullingDown()是下拉過程中ACTION_MOVE時(shí)的回調(diào)方法,onPullReleasing()是下拉狀態(tài)中ACTION_UP時(shí)的回調(diào)方法,startAnim()則是正在刷新時(shí)回調(diào)的方法。其中fraction=mChildView.getTranslationY()/mHeadHeight,fraction=1 時(shí),mChildView的位移恰好是HeadLayout的高度,fraction>1 時(shí)則超過了HeadLayout的高度,其最大高度可以到達(dá) mWaveHeight/mHeadHeight。這樣我們只需要寫一個(gè)View來實(shí)現(xiàn)這個(gè)接口就可以實(shí)現(xiàn)個(gè)性化了,該有的參數(shù)都有了!

8. 實(shí)現(xiàn)越界回彈

不能在手指快速滾動(dòng)到頂部時(shí)對(duì)越界做出反饋,這是一個(gè)繼承及ViewGroup的刷新控件的通病。沒有繼承自具體的列表控件,它沒辦法獲取到列表控件的Scroller,不能獲取到列表控件的當(dāng)前滾動(dòng)速度,更是不能預(yù)知列表控件什么時(shí)候能滾動(dòng)到頂部;同時(shí)ViewGroup除了達(dá)到臨界狀態(tài)的事件被攔截了,其它事件全都交給了子View去處理。我們能獲取到的有關(guān)于子View的操作,只有簡(jiǎn)簡(jiǎn)單單的手指的觸摸事件。so, let's do it!

  1. mChildView.setOnTouchListener(new OnTouchListener() { 
  2.     @Override 
  3.     public boolean onTouch(View v, MotionEvent event) { 
  4.         return gestureDetector.onTouchEvent(event); 
  5.     } 
  6. });  

我們把在mChildView上的觸摸事件交給了一個(gè)工具類GestureDetector去處理,它可以輔助檢測(cè)用戶的單擊、滑動(dòng)、長(zhǎng)按、雙擊、快速滑動(dòng)等行為。我們這里只需要重寫onFling()方法并獲取到手指在Y方向上的速度velocityY,要是再能及時(shí)的發(fā)現(xiàn)mChildView滾動(dòng)到了頂部就可以解決問題了。

  1. GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { 
  2.  
  3.         @Override 
  4.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
  5.             mVelocityY = velocityY; 
  6.         } 
  7.     });  

此外獲取速度還可以用VelocityTracker,比較麻煩一些:

  1. VelocityTracker tracker = VelocityTracker.obtain(); 
  2. tracker.addMovement(ev); 
  3. //然后在恰當(dāng)?shù)奈恢檬褂萌缦路椒ǐ@取速度 
  4. tracker.computeCurrentVelocity(1000); 
  5. mVelocityY = (int)tracker.getYVelocity();  

繼續(xù)來實(shí)現(xiàn)越界回彈。對(duì)于RecyclerView、AbsListView,它們提供有OnScrollListener可以獲取一下滾動(dòng)狀態(tài):

  1. if (mChildView instanceof RecyclerView) { 
  2.             ((RecyclerView) mChildView).addOnScrollListener(new RecyclerView.OnScrollListener() { 
  3.                 @Override 
  4.                 public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
  5.                     if (!isRefreshing && !isLoadingmore && newState == RecyclerView.SCROLL_STATE_IDLE) { 
  6.                         if (mVelocityY >= 5000 && ScrollingUtil.isRecyclerViewToTop((RecyclerView) mChildView)) { 
  7.                             animOverScrollTop(); 
  8.                         } 
  9.                         if (mVelocityY <= -5000 && ScrollingUtil.isRecyclerViewToBottom((RecyclerView) mChildView)) { 
  10.                             animOverScrollBottom(); 
  11.                         } 
  12.                     } 
  13.                     super.onScrollStateChanged(recyclerView, newState); 
  14.                 } 
  15.             }); 
  16.         }  

筆者選取了一個(gè)滾動(dòng)速度的臨界值,Y方向的滾動(dòng)速度大于5000時(shí)才允許越界回彈,RecyclerView的OnScrollListener可以讓我們獲取到滾動(dòng)狀態(tài)的改變,滾動(dòng)到頂部時(shí)滾動(dòng)完成,狀態(tài)變?yōu)镾CROLL_STATE_IDLE,執(zhí)行越界回彈動(dòng)畫。這樣的策略也還有一些缺陷,不能獲取到mChildView滾動(dòng)到頂部時(shí)的滾動(dòng)速度,也就不能根據(jù)不同的滾動(dòng)速度來實(shí)現(xiàn)更加友好的越界效果?,F(xiàn)在的越界高度是固定的,還需要后面進(jìn)行優(yōu)化,比如采用加速度來計(jì)算,是否可行還待驗(yàn)證。

9. 滾動(dòng)的延時(shí)計(jì)算策略

上面的方法對(duì)于RecyclerView和AbsListView都好用,對(duì)于ScrollView、WebView就頭疼了,只能使用延時(shí)計(jì)算一段時(shí)間看有沒有到達(dá)頂部的方式來判斷的策略。延時(shí)策略的思想就是通過發(fā)送一系列的延時(shí)消息從而達(dá)到一種漸進(jìn)式計(jì)算的效果,具體來說可以使用Handler或View的postDelayed方法,也可以使用線程的sleep方法。另外提一點(diǎn),需要不斷循環(huán)計(jì)算一個(gè)數(shù)值,比如自定義View需要實(shí)現(xiàn)根據(jù)某個(gè)數(shù)值變化的動(dòng)效,最好不要使用Thread + while 循環(huán)的方式計(jì)算,使用ValueAnimator會(huì)是更好的選擇。這里筆者選擇了Handler的方式。

  1. @Override 
  2. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
  3.     mVelocityY = velocityY; 
  4.     if (!(mChildView instanceof AbsListView || mChildView instanceof RecyclerView)) { 
  5.         //既不是AbsListView也不是RecyclerView,由于這些沒有實(shí)現(xiàn)OnScrollListener接口,無法回調(diào)狀態(tài),只能采用延時(shí)策略 
  6.         if (Math.abs(mVelocityY) >= 5000) { 
  7.             mHandler.sendEmptyMessage(MSG_START_COMPUTE_SCROLL); 
  8.         } else { 
  9.             cur_delay_times = ALL_DELAY_TIMES; 
  10.         } 
  11.     } 
  12.     return false
  13.  

在滾動(dòng)速度大于5000的時(shí)候發(fā)送一個(gè)重新計(jì)算的消息,Handler收到消息后,延時(shí)一段時(shí)間繼續(xù)給自己發(fā)送消息,直到時(shí)間用完或者mChildView滾動(dòng)到頂部或者用戶又進(jìn)行了一次Fling動(dòng)作。 

  1. private Handler mHandler = new Handler() { 
  2.     @Override 
  3.     public void handleMessage(Message msg) { 
  4.         switch (msg.what) { 
  5.             case MSG_START_COMPUTE_SCROLL: 
  6.                 cur_delay_times = -1; //這里沒有break,寫作-1方便計(jì)數(shù) 
  7.             case MSG_CONTINUE_COMPUTE_SCROLL: 
  8.                 cur_delay_times++; 
  9.  
  10.                 if (!isRefreshing && !isLoadingmore && mVelocityY >= 5000 && childScrollToTop()) { 
  11.                     animOverScrollTop(); 
  12.                     cur_delay_times = ALL_DELAY_TIMES; 
  13.                 } 
  14.  
  15.                 if (!isRefreshing && !isLoadingmore && mVelocityY <= -5000 && childScrollToBottom()) { 
  16.                     animOverScrollBottom(); 
  17.                     cur_delay_times = ALL_DELAY_TIMES; 
  18.                 } 
  19.  
  20.                 if (cur_delay_times < ALL_DELAY_TIMES) 
  21.                     mHandler.sendEmptyMessageDelayed(MSG_CONTINUE_COMPUTE_SCROLL, 10); 
  22.                 break; 
  23.             case MSG_STOP_COMPUTE_SCROLL: 
  24.                 cur_delay_times = ALL_DELAY_TIMES; 
  25.                 break; 
  26.         } 
  27.     } 
  28. };  

ALL_DELAY_TIMES是最多可以計(jì)算的次數(shù),當(dāng)Handler接收到MSG_START_COMPUTE_SCROLL消息時(shí),如果mChildView沒有滾動(dòng)到邊界處,則會(huì)在10ms之后向自己發(fā)送一條MSG_CONTINUE_COMPUTE_SCROLL的消息,然后繼續(xù)進(jìn)行判斷。然后在合適的時(shí)候越界回彈就好了。

10. 實(shí)現(xiàn)個(gè)性化Header

這里筆者來演示一下,怎么輕輕松松的做一個(gè)個(gè)性化的Header,比如新浪微博樣式的刷新Header(如下面第1圖)。

  1. 創(chuàng)建 SinaRefreshView 繼承自 FrameLayout 并實(shí)現(xiàn) IHeaderView 接口
  2. getView()方法中返回this
  3. 在onAttachedToWindow()方法中獲取一下需要用到的布局(筆者寫到了xml中,也可以直接在代碼里面寫)
    1. @Override 
    2. protected void onAttachedToWindow() { 
    3.     super.onAttachedToWindow(); 
    4.  
    5.     if (rootView == null) { 
    6.         rootView = View.inflate(getContext(), R.layout.view_sinaheader, null); 
    7.         refreshArrow = (ImageView) rootView.findViewById(R.id.iv_arrow); 
    8.         refreshTextView = (TextView) rootView.findViewById(R.id.tv); 
    9.         loadingView = (ImageView) rootView.findViewById(R.id.iv_loading); 
    10.         addView(rootView); 
    11.     } 
  4. 實(shí)現(xiàn)其它方法
    1. @Override 
    2. public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) { 
    3.     if (fraction < 1f) refreshTextView.setText(pullDownStr); 
    4.     if (fraction > 1f) refreshTextView.setText(releaseRefreshStr); 
    5.     refreshArrow.setRotation(fraction * headHeight / maxHeadHeight * 180); 
    6.  
    7. @Override 
    8. public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) { 
    9.     if (fraction < 1f) { 
    10.         refreshTextView.setText(pullDownStr); 
    11.         refreshArrow.setRotation(fraction * headHeight / maxHeadHeight * 180); 
    12.         if (refreshArrow.getVisibility() == GONE) { 
    13.             refreshArrow.setVisibility(VISIBLE); 
    14.             loadingView.setVisibility(GONE); 
    15.         } 
    16.     } 
    17.  
    18. @Override 
    19. public void startAnim(float maxHeadHeight, float headHeight) { 
    20.     refreshTextView.setText(refreshingStr); 
    21.     refreshArrow.setVisibility(GONE); 
    22.     loadingView.setVisibility(VISIBLE); 
  5. 布局文件
    1. <?xml version="1.0" encoding="utf-8"?> 
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    3.     android:orientation="horizontal" android:layout_width="match_parent" 
    4.     android:layout_height="match_parent" 
    5.     android:gravity="center"
    6.     <ImageView 
    7.         android:id="@+id/iv_arrow" 
    8.         android:layout_width="wrap_content" 
    9.         android:layout_height="wrap_content" 
    10.         android:src="@drawable/ic_arrow"/> 
    11.  
    12.     <ImageView 
    13.         android:id="@+id/iv_loading" 
    14.         android:visibility="gone" 
    15.         android:layout_width="34dp" 
    16.         android:layout_height="34dp" 
    17.         android:src="@drawable/anim_loading_view"/> 
    18.  
    19.     <TextView 
    20.         android:id="@+id/tv" 
    21.         android:layout_width="wrap_content" 
    22.         android:layout_height="wrap_content" 
    23.         android:layout_marginLeft="16dp" 
    24.         android:textSize="16sp" 
    25.         android:text="下拉刷新"/> 
    26. </LinearLayout> 

注意fraction的使用,比如上面的代碼 refreshArrow.setRotation(fraction headHeight / maxHeadHeight 180),fraction * headHeight表示當(dāng)前頭部滑動(dòng)的距離,然后算出它和最大高度的比例,然后乘以180,可以使得在滑動(dòng)到最大距離時(shí)Arrow恰好能旋轉(zhuǎn)180度。startAnim()方法是在onRefresh之后會(huì)自動(dòng)調(diào)用的方法。

要想實(shí)現(xiàn)如圖2所示效果,可以具體查看筆者的開源庫TwinklingRefreshLayout。

 

 

總結(jié)

至此,筆者實(shí)現(xiàn)這個(gè)刷新控件的所有核心思想都講完了,其中并沒有用到多么高深的技術(shù),只是需要我們多一點(diǎn)耐心,多去調(diào)試,不要逃避bug,多挑戰(zhàn)一下自己。

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2018-08-03 12:21:02

2018-01-09 15:44:57

2018-01-05 15:36:12

工具博客寫作

2021-04-16 09:17:39

機(jī)器學(xué)習(xí)人工智能AI

2020-07-21 08:42:16

搞垮服務(wù)器日志

2021-10-20 07:48:17

DatalistCSS技巧

2022-08-24 16:26:51

Linuxcheat 命令

2019-10-28 11:30:43

架構(gòu)數(shù)據(jù)結(jié)構(gòu)布隆過濾器

2019-10-31 10:43:05

Python 開發(fā)編程語言

2021-08-02 07:23:54

爬蟲requests網(wǎng)絡(luò)庫

2022-11-04 13:06:47

JVMJava程序

2020-04-01 11:12:43

腦機(jī)接口機(jī)器翻譯人工智能

2025-02-08 10:29:03

2018-07-26 08:42:11

2012-11-12 09:44:43

2020-02-25 23:36:04

代碼開發(fā)工具

2020-10-24 20:10:40

Python 開發(fā)編程語言

2021-03-09 16:36:21

大數(shù)據(jù)薪資工作

2019-04-16 08:50:56

WebHTTP緩存

2019-06-10 13:50:08

Linux命令shell
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)