Skip to content

Instantly share code, notes, and snippets.

@s1ntoneli
Last active October 15, 2017 12:07
Show Gist options
  • Select an option

  • Save s1ntoneli/09ef08a7e8cee7b8ea8e2d9b55a89b0b to your computer and use it in GitHub Desktop.

Select an option

Save s1ntoneli/09ef08a7e8cee7b8ea8e2d9b55a89b0b to your computer and use it in GitHub Desktop.
极简的带加载更多的SwipeRefreshLayout

用法

1.添加布局 方式同 SwipeRefreshLayout

<SwipeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclveVew"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</SwipeLayout>

2.实现并设置SwipeContent

public interface SwipeContent {
    // 是否开启加载更多功能
    boolean canLoad();
    // 是否到了底部
    boolean isBottom();
}

3.设置 OnLoadListener

public interface OnLoadListener {
    void onLoad();
}

RecyclerView为例

final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);   
mSwipeLayout.setSwipeContent(new SwipeLayout.SwipeContent() {
    @Override
    public boolean canLoad() {
        return true;
    }

    @Override
    public boolean isBottom() {
        // isBottom if the last item is visible
        int lastVisibleCount = layoutManager.findLastVisibleItemPosition();
        int totalItemCount = layoutManager.getItemCount();
        return lastVisibleCount >= totalItemCount - 1;
    }
});

mSwipeLayout.setOnLoadListener(new SwipeLayout.OnLoadListener() {
    @Override
    public void onLoad() {
        // do loading things here
        ...
        // stop loading
        mSwipeLayout.stopLoading(false);
    }
});

设计思路

设计最初是希望加载更多的使用方法和下拉刷新相同,像下面的方式:

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        // do refresh things here
        ...
    }
});

因此对应的加载更多就是:

mSwipeLayout.setOnLoadListener(new SwipeLayout.OnLoadListener() {
    @Override
    public void onLoad() {
        // do loading things here
        ...
    }
});

那么设计思路就出来了: SwipeLayout 监听onScroll方法 => 调用用户为子 View 重写的SwipeContent.isBottom方法确认是否到底部 => 若是到底部,调用OnLoadListener.onLoad()

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
/**
* Created by lixindong on 5/27/17.
*/
public class SwipeLayout extends SwipeRefreshLayout implements ViewTreeObserver.OnScrollChangedListener {
private OnLoadListener mOnLoadListener;
public SwipeLayout(Context context) {
this(context, null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
getViewTreeObserver().addOnScrollChangedListener(this);
}
@Override
public void onScrollChanged() {
if (!mIsLoading && mSwipeContent.isBottom() && mSwipeContent.canLoad()) {
if (mOnLoadListener != null) {
mIsLoading = true;
mOnLoadListener.onLoad();
}
}
}
public interface OnLoadListener {
void onLoad();
}
public void setOnLoadListener(OnLoadListener listener) {
mOnLoadListener = listener;
}
private SwipeContent mSwipeContent;
private boolean mIsLoading = false;
public interface SwipeContent {
boolean canLoad();
boolean isBottom();
}
public void setSwipeContent(SwipeContent swipeContent) {
mSwipeContent = swipeContent;
}
public boolean isLoading() {
return mIsLoading;
}
public void setLoading(boolean loading) {
mIsLoading = loading;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment