刷新框架那么多喷楣,為什么我選擇了SwipeToLoadLayout?
首先看效果鹤树,框架中幫我們實現(xiàn)了幾個主流的刷新效果铣焊,Twitter style,JD style罕伯,google style曲伊,Yalantis style,demo也下載下來看了追他,真不錯坟募,還支持各種自定義,自定義頭部和尾部邑狸,頭部還分classic懈糯,above,blow单雾,scale四種類型赚哗,還有自動刷新的效果,體驗也很流暢硅堆。
再看代碼蜂奸,刷新,加載各一個接口實現(xiàn)硬萍,頭部和尾部也都是用接口實現(xiàn)扩所,遵循設(shè)計模式的依賴倒轉(zhuǎn)原則原則(針對抽象而不是針對具體編程),所以才能具備那么高可塑性朴乖,我們要做的就是實現(xiàn)接口里面的內(nèi)容就可以輕松寫一個刷新效果祖屏,就像使用baseAdapter一樣助赞,無論什么數(shù)據(jù),什么樣式袁勺,都可以輕松實現(xiàn)雹食。
接著看功能,支持各種View和ViewGroup(ListView期丰,ScrollView群叶,RecyclerView,GridView钝荡,WebView街立,Linearlayout,RelativeLayout埠通,F(xiàn)rameLayout赎离,ImageView,TextView等)的刷新和加載端辱,還支持自動刷新梁剔,手動刷新,自動加載舞蔽,手動加載荣病,禁止刷新,禁止加載等操作渗柿,完全滿足需求个盆。
最后脐湾,說的這么好,有沒有經(jīng)過測試呢谅将?當(dāng)然了中跌,口說無憑,帶大家實現(xiàn)一個沼死。
通過SwipeToLoadLayout實現(xiàn)一個刷新加載的效果
1、如何集成
Step 1. Add the JitPack repository in your build.gradle at the end of repositories:
repositories{maven{url"https://jitpack.io"}}
Step 2. Add the dependency in the form
dependencies{compile'com.github.Aspsine:SwipeToLoadLayout:v1.0.0'}
2,開始自定義刷新效果
swipeToLoadLayout提供了一套接口声登,刷新的頭部自定義一個View實現(xiàn)SwipeTrigger和SwipeRefreshTrigger就行了,刷新的尾部自定義一個View實現(xiàn)SwipeLoadMoreTrigger和SwipeTrigger揣苏,頭部實現(xiàn)代碼:
publicclassCustomRefreshHeadViewextendsTextViewimplementsSwipeRefreshTrigger,SwipeTrigger{publicCustomRefreshHeadView(Context context){super(context);? ? }publicCustomRefreshHeadView(Context context, AttributeSet attrs){super(context, attrs);? ? }publicCustomRefreshHeadView(Context context, AttributeSet attrs,intdefStyleAttr){super(context, attrs, defStyleAttr);? ? }@OverridepublicvoidonRefresh(){? ? ? ? setText("正在拼命加載數(shù)據(jù)...");? ? }@OverridepublicvoidonPrepare(){? ? }@OverridepublicvoidonSwipe(inti,booleanb){? ? ? ? setText("釋放刷新");? ? }@OverridepublicvoidonRelease(){? ? }@Overridepublicvoidcomplete(){? ? ? ? setText("刷新成功");? ? }@OverridepublicvoidonReset(){? ? }}
Xml中使用
注意悯嗓,swipetoloadlayout中布局包裹的View id是指定的,不能亂改卸察,否則找不到刷新目標(biāo)
刷新頭部
刷新尾部
代碼中調(diào)用
CustomRefreshHeadViewrefreshHeadView =newCustomRefreshHeadView(this);refreshHeadView.setPadding(20,20,20,20);refreshHeadView.setGravity(Gravity.CENTER);refreshHeadView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));swipeToLoadLayout.setRefreshHeaderView(refreshHeadView);
就這么簡單脯厨,看下演示效果,做的丑了點坑质,以后有時間弄個精致點的
swipeToRefresh.gif
就這么簡單合武,大伙可以試試临梗,更多用法查看原作者項目demo。