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()