1、概述
SwipeRefreshLayout是一種下拉刷新的組件,它被放到了V4包中,只允許有一個直接子類。
如下:mTarget是SwipeRefreshLayout的目標(biāo)View,它是SwipeRefreshLayout的第一個子view,

SwipeRefreshLayout源碼
2、簡單的使用方法
如下:
SwipeRefreshLayout swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
//setColorSchemeResources()可以改變加載圖標(biāo)的顏色。
swipeRefreshLayout.setColorSchemeResources(new int[]{R.color.colorAccent, R.color.colorPrimary});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(false);
}
});
3、顯示空布局
但是,當(dāng)沒有數(shù)據(jù)的時候,怎么顯示出數(shù)據(jù)為空的布局?可以查看google官方的例子,如下:
google官方例子
- 調(diào)用方法:
View mEmptyView = findViewById(android.R.id.empty);
loadMoreListView.setEmptyView(mEmptyView);
swipeRefreshLayout.setSwipeableChildren(R.id.LoadMoreListView,android.R.id.empty);
- 布局文件
<com.liyi.loadmorelistview.MultiSwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/LoadMoreListView"
android:layout_width="match_parent"
android:divider="@color/colorAccent"
android:footerDividersEnabled="true"
android:dividerHeight="2dp"
android:listSelector="@android:color/transparent"
android:layout_height="match_parent"/>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List is empty! Click here to refresh."
android:layout_gravity="center"/>
</FrameLayout>
</com.liyi.loadmorelistview.MultiSwipeRefreshLayout>
如果沒有自定義SwipeRefreshLayout,會出現(xiàn)即便沒有到頂部,SwipeRefreshLayout也會刷新,正確的情況應(yīng)該是:當(dāng)ListView位于頂部時,下拉事件由SwipeRefreshLayout處理,否則由ListView處理

只要下拉,就觸發(fā)刷新
- 自定義SwipeRefreshLayout
重寫canChildScrollUp()方法,返回true,不進(jìn)行刷新;false進(jìn)行刷新。

onInterceptTouchEvent方法
public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
private View[] mSwipeableChildren;
public MultiSwipeRefreshLayout(Context context) {
super(context);
}
public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 設(shè)置可以觸發(fā)刷新事件的子view
* Set the children which can trigger a refresh by swiping down when they are visible. These
* views need to be a descendant of this view.
*/
public void setSwipeableChildren(final int... ids) {
assert ids != null;
mSwipeableChildren = new View[ids.length];
for (int i = 0; i < ids.length; i++) {
mSwipeableChildren[i] = findViewById(ids[i]);
}
}
/**
* 如果返回false,開始gesture
* <p/>
* This method controls when the swipe-to-refresh gesture is triggered. By returning false here
* we are signifying that the view is in a state where a refresh gesture can start.
* <p/>
* <p>As {@link android.support.v4.widget.SwipeRefreshLayout} only supports one direct child by
* default, we need to manually iterate through our swipeable children to see if any are in a
* state to trigger the gesture. If so we return false to start the gesture.
*/
@Override
public boolean canChildScrollUp() {
if (mSwipeableChildren != null && mSwipeableChildren.length > 0) {
for (View view : mSwipeableChildren) {
if (view != null && view.isShown()) {
if (view instanceof AbsListView) {
return canViewScrollUp(view);
}
}
}
}
return true;
}
private static boolean canViewScrollUp(View view) {
//負(fù)數(shù),表示view上滑
return ViewCompat.canScrollVertically(view, -1);
}
}

沒有數(shù)據(jù)

正在加載數(shù)據(jù)

有數(shù)據(jù)
在SwipeRefreshLayout中加入多個子View
Android中SwipeRefreshLayout和listview的沖突解決辦法
4、關(guān)于SwipeRefreshLayout+ViewPager的滑動沖突
- 思路:因?yàn)槭窍吕⑿?,只有縱向滑動的時候才有效,可以通過判斷此時是縱向滑動還是橫向滑動(X軸的移動距離大于Y軸的移動距離),縱向滑動就攔截事件,橫向滑動不攔截。
- 可以重寫onInterceptTouchEvent()方法,橫向滑動不攔截(return false)
代碼如下:
public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
private float startY;
private float startX;
// 記錄viewPager是否拖拽的標(biāo)記
private boolean mIsBeingDragged;
private final int mTouchSlop;
private View mSwipeableChildren;
public MultiSwipeRefreshLayout(Context context) {
this(context, null);
}
public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float currentX=ev.getX();
float currentY=ev.getY();
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
startY=currentY;
startX=currentX;
mIsBeingDragged=false;
break;
case MotionEvent.ACTION_MOVE:
//如果viewpager正在拖拽中,那么不攔截它的事件,直接return false;
if(mIsBeingDragged){
return false;
}
float dx=Math.abs(currentX-startX);
float dy=Math.abs(currentY-startY);
// 如果X軸位移大于Y軸位移,那么將事件交給viewPager處理。
if(dx>dy && dx>mTouchSlop){
mIsBeingDragged=true;
return false;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mIsBeingDragged=false;
break;
}
// 如果是Y軸位移大于X軸,事件交給swipeRefreshLayout處理。
return super.onInterceptTouchEvent(ev);
}
/**
* 傳入SwipeRefreshLayout的Target的id,如R.id.viewPager
* swipeRefreshLayout.setSwipeableChildren(R.id.viewPager);
* @param ids
*/
public void setSwipeableChildren(final int ids) {
mSwipeableChildren = findViewById(ids);
}
@Override
public boolean canChildScrollUp() {
return canViewScrollUp(mSwipeableChildren);
}
/**
* 根據(jù)傳入的ViewPager,查找內(nèi)部的ListView,并判斷其是否到頂部
* @param view
* @return
*/
private static boolean canViewScrollUp(View view) {
if (view!=null && view instanceof ViewPager) {
for (int i = 0; i < ((ViewPager) view).getChildCount(); i++) {
View child = ((ViewPager) view).getChildAt(i);
if (child.isShown()) {
if (child instanceof RelativeLayout) {
View subChild = ((RelativeLayout) child).getChildAt(0);
if (subChild instanceof AbsListView) {
final AbsListView listView = (AbsListView) subChild;
return ViewCompat.canScrollVertically(listView, -1);
}
}
}
}
}
return false;
}
}

SwipeRefreshLayout+ViewPager---上拉
參考:
Android:SwipeRefreshLayout和ViewPager滑動沖突的原因和正確的解決方式
5、帶上拉功能的SwipeRefreshLayout
地址為 :https://github.com/OrangeGangsters/SwipyRefreshLayout

帶上拉功能的SwipeRefreshLayout