剛開始接觸android的時(shí)候,就想著如果能定制一款屬于自己的刷新效果,肯定會是一件很酷的事情,當(dāng)然了,github上已經(jīng)有了很多很炫酷的刷新效果,各種漂亮,但在項(xiàng)目中總要講究個(gè)實(shí)用性,有些刷新效果是不錯(cuò),但在實(shí)戰(zhàn)中就會顯得很不搭,無賴只能舍棄,XListView實(shí)用,PullToRefresh也實(shí)用,用了這么久,是真不想用,在當(dāng)時(shí)是很酷,可這些項(xiàng)目也早就沒人維護(hù),刷新效果有些過時(shí),修改起來也不是那么容易,項(xiàng)目中經(jīng)常要趕進(jìn)度,so,也一直將就著用,google 提供的SwipeRefreshLayout效果也不賴,但卻很局限,樣式局限,子布局還一定得是可以滑動(dòng)的布局(如ListView,ScrollView),還沒提供上拉加載的效果,要用還得自己寫,還有一點(diǎn)不知道google怎么搞的,SwipeRefreshLayout居然和RecyclerView沖突(連續(xù)下拉,刷新樣式會一團(tuán)糟,還會卡?。约铱丶€弄成這樣,真是的。說了那么一堆,來看看今天的主角SwipeToLoadLayout,獻(xiàn)上兩張截圖:

刷新框架那么多,為什么我選擇了SwipeToLoadLayout?
首先看效果,框架中幫我們實(shí)現(xiàn)了幾個(gè)主流的刷新效果,Twitter style,JD style,google style,Yalantis style,demo也下載下來看了,真不錯(cuò),還支持各種自定義,自定義頭部和尾部,頭部還分classic,above,blow,scale四種類型,還有自動(dòng)刷新的效果,體驗(yàn)也很流暢。
再看代碼,刷新,加載各一個(gè)接口實(shí)現(xiàn),頭部和尾部也都是用接口實(shí)現(xiàn),遵循設(shè)計(jì)模式的依賴倒轉(zhuǎn)原則原則(針對抽象而不是針對具體編程),所以才能具備那么高可塑性,我們要做的就是實(shí)現(xiàn)接口里面的內(nèi)容就可以輕松寫一個(gè)刷新效果,就像使用baseAdapter一樣,無論什么數(shù)據(jù),什么樣式,都可以輕松實(shí)現(xiàn)。
接著看功能,支持各種View和ViewGroup(ListView,ScrollView,RecyclerView,GridView,WebView,Linearlayout,RelativeLayout,F(xiàn)rameLayout,ImageView,TextView等)的刷新和加載,還支持自動(dòng)刷新,手動(dòng)刷新,自動(dòng)加載,手動(dòng)加載,禁止刷新,禁止加載等操作,完全滿足需求。
最后,說的這么好,有沒有經(jīng)過測試呢?當(dāng)然了,口說無憑,帶大家實(shí)現(xiàn)一個(gè)。
通過SwipeToLoadLayout實(shí)現(xiàn)一個(gè)刷新加載的效果
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提供了一套接口,刷新的頭部自定義一個(gè)View實(shí)現(xiàn)SwipeTrigger和SwipeRefreshTrigger就行了,刷新的尾部自定義一個(gè)View實(shí)現(xiàn)SwipeLoadMoreTrigger和SwipeTrigger,頭部實(shí)現(xiàn)代碼:
public class CustomRefreshHeadView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {
public CustomRefreshHeadView(Context context) {
super(context);
}
public CustomRefreshHeadView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRefreshHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onRefresh() {
setText("正在拼命加載數(shù)據(jù)...");
}
@Override
public void onPrepare() {
}
@Override
public void onSwipe(int i, boolean b) {
setText("釋放刷新");
}
@Override
public void onRelease() {
}
@Override
public void complete() {
setText("刷新成功");
}
@Override
public void onReset() {
}
}
Xml中使用
注意,swipetoloadlayout中布局包裹的View id是指定的,不能亂改,否則找不到<item name="swipe_target" type="id" />刷新目標(biāo)
<item name="swipe_refresh_header" type="id" />刷新頭部
<item name="swipe_load_more_footer" type="id" />刷新尾部
<?xml version="1.0" encoding="utf-8"?>
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeToLoad"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yyydjk.swipetorefreshdemo.MainActivity">
<com.yyydjk.swipetorefreshdemo.CustomRefreshHeadView
android:id="@+id/swipe_refresh_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp" />
<TextView
android:id="@+id/swipe_target"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="Hello World!" />
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
代碼中調(diào)用
CustomRefreshHeadView refreshHeadView = new CustomRefreshHeadView(this);
refreshHeadView.setPadding(20,20,20,20);
refreshHeadView.setGravity(Gravity.CENTER);
refreshHeadView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
swipeToLoadLayout.setRefreshHeaderView(refreshHeadView);
就這么簡單,看下演示效果,做的丑了點(diǎn),以后有時(shí)間弄個(gè)精致點(diǎn)的

就這么簡單,大伙可以試試,更多用法查看原作者項(xiàng)目demo。
希望大家多多關(guān)注我,好東西,分享才有價(jià)值