【Android-自定义控件】SwipeRefreshDemo 下拉刷新,上拉加载
参考:https://github.com/PingerOne/SwipeRefreshDemo
谷歌官方的SwipeRefreshLayout控件,只有下拉刷新功能。
自定义的SwipeRefreshView ,继承自SwipeRefreshLayout,添加了上拉加载更多功能,添加对RecyclerView的支持。
添加“加载更多”底部布局 swiperefreshview_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<ProgressBar
android:id="@+id/load_progress"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="正在努力加载中..."
android:textColor="@android:color/black"
android:textSize="16sp"/>
</RelativeLayout>
</RelativeLayout>
自定义view - SwipeRefreshView
/**
* 自定义View继承SwipeRefreshLayout
* 添加上拉加载更多的布局属性
* 添加对RecyclerView的支持
*/
public class SwipeRefreshView extends SwipeRefreshLayout {
private static final String TAG = SwipeRefreshView.class.getSimpleName();
private final int mScaledTouchSlop;
private final View mFooterView;
private ListView mListView;
private OnLoadMoreListener mListener;
/**
* 正在加载状态
*/
private boolean isLoading;
private RecyclerView mRecyclerView;
private int mItemCount;
public SwipeRefreshView(Context context, AttributeSet attrs) {
super(context, attrs);
// 填充底部加载布局
mFooterView = View.inflate(context, R.layout.swiperefreshview_footer, null);
// 表示控件移动的最小距离,手移动的距离大于这个距离才能拖动控件
mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// 获取ListView,设置ListView的布局位置
if (mListView == null || mRecyclerView == null) {
// 判断容器有多少个孩子
if (getChildCount() > 0) {
// 判断第一个孩子是不是ListView
if (getChildAt(0) instanceof ListView) {
// 创建ListView对象
mListView = (ListView) getChildAt(0);
// 设置ListView的滑动监听
setListViewOnScroll();
} else if (getChildAt(0) instanceof RecyclerView) {
// 创建ListView对象
mRecyclerView = (RecyclerView) getChildAt(0);
// 设置RecyclerView的滑动监听
setRecyclerViewOnScroll();
}
}
}
}
/**
* 在分发事件的时候处理子控件的触摸事件
*/
private float mDownY, mUpY;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 移动的起点
mDownY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
// 移动过程中判断时候能下拉加载更多
if (canLoadMore()) {
// 加载数据
loadData();
}
break;
case MotionEvent.ACTION_UP:
// 移动的终点
mUpY = getY();
break;
}
return super.dispatchTouchEvent(ev);
}
/**
* 判断是否满足加载更多条件
*/
private boolean canLoadMore() {
// 1. 是上拉状态
boolean condition1 = (mDownY - mUpY) >= mScaledTouchSlop;
if (condition1) {
Log.d(TAG, "-------> 是上拉状态");
}
// 2. 当前页面可见的item是最后一个条目,一般最后一个条目位置需要大于第一页的数据长度
boolean condition2 = false;
if (mListView != null && mListView.getAdapter() != null) {
if (mItemCount > 0) {
if (mListView.getAdapter().getCount() < mItemCount) {
// 第一页未满,禁止下拉
condition2 = false;
}else {
condition2 = mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
}
} else {
// 未设置数据长度,则默认第一页数据不满时也可以上拉
condition2 = mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
}
}
if (condition2) {
Log.d(TAG, "-------> 是最后一个条目");
}
// 3. 正在加载状态
boolean condition3 = !isLoading;
if (condition3) {
Log.d(TAG, "-------> 不是正在加载状态");
}
return condition1 && condition2 && condition3;
}
public void setItemCount(int itemCount) {
this.mItemCount = itemCount;
}
/**
* 处理加载数据的逻辑
*/
private void loadData() {
System.out.println("加载数据...");
if (mListener != null) {
// 设置加载状态,让布局显示出来
setLoading(true);
mListener.onLoadMore();
}
}
/**
* 设置加载状态,是否加载传入boolean值进行判断
*
* @param loading
*/
public void setLoading(boolean loading) {
// 修改当前的状态
isLoading = loading;
if (isLoading) {
// 显示布局
mListView.addFooterView(mFooterView);
} else {
// 隐藏布局
mListView.removeFooterView(mFooterView);
// 重置滑动的坐标
mDownY = 0;
mUpY = 0;
}
}
/**
* 设置ListView的滑动监听
*/
private void setListViewOnScroll() {
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 移动过程中判断时候能下拉加载更多
if (canLoadMore()) {
// 加载数据
loadData();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
/**
* 设置RecyclerView的滑动监听
*/
private void setRecyclerViewOnScroll() {
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// 移动过程中判断时候能下拉加载更多
if (canLoadMore()) {
// 加载数据
loadData();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
/**
* 上拉加载的接口回调
*/
public interface OnLoadMoreListener {
void onLoadMore();
}
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
this.mListener = listener;
}
}
应用布局
<com.example.customview.SwipeRefreshView
android:id="@+id/swipeRefreshView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</com.example.customview.SwipeRefreshView>
应用代码
/**
* 使用自定义的SwipeRefreshView
*/
public class RefreshActicity extends AppCompatActivity {
SwipeRefreshView swipeRefreshView;
ListView listView;
List<Product> list;
ProductAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_refresh);
swipeRefreshView = findViewById(R.id.swipeRefreshView);
listView = findViewById(R.id.listView);
//设置适配器
list=new ArrayList<>();
adapter = new ProductAdapter(this, list);
listView.setAdapter(adapter);
//设置下拉进度的背景颜色,默认是白色
swipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
//设置下拉进度的肢体颜色
swipeRefreshView.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
swipeRefreshView.setItemCount(20);
// 手动调用,通知系统去测量
swipeRefreshView.measure(0,0);
swipeRefreshView.setRefreshing(true);
initData();
//下拉时触发SwipeRefreshLayout的下拉动画,动画完毕后回调这个方法
swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
initData();
}
});
//设置下拉加载更多
swipeRefreshView.setOnLoadMoreListener(new SwipeRefreshView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
loadMoreData();
}
});
}
/**
* 第一次刷新数据
*/
private void initData()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
list.addAll(DataResource.getData());
adapter.notifyDataSetChanged();
//加载完数据,设置为不刷新状态,将下拉进度收起来
swipeRefreshView.setRefreshing(false);
Toast.makeText(RefreshActicity.this, "刷新了20条数据", Toast.LENGTH_SHORT).show();
// 加载完数据设置为不刷新状态,将下拉进度收起来
if (swipeRefreshView.isRefreshing()) {
swipeRefreshView.setRefreshing(false);
}
}
}, 2000);
}
/**
* 加载更多数据
*/
private void loadMoreData()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
list.addAll(DataResource.getMoreData());
Toast.makeText(RefreshActicity.this, "加载了" + 20 + "条数据", Toast.LENGTH_SHORT).show();
// 加载完数据设置为不加载状态,将加载进度收起来
swipeRefreshView.setLoading(false);
}
}, 2000);
}
public static class DataResource {
private static List<Product> datas = new ArrayList<>();
private static int page = 0;
//第一次加载数据
public static List<Product> getData() {
page = 0;
datas.clear();
for (int i = 0; i < 20; i++) {
Random random = new Random();
Product product = new Product("100" + (i+1), "testdata", random.nextInt(100));
datas.add(product);
}
return datas;
}
//加载更多数据
public static List<Product> getMoreData() {
page = page + 1;
for (int i = 20 * page; i < 20 * (page + 1); i++) {
Random random = new Random();
Product product = new Product("100" + (i+1), "testdata", random.nextInt(100));
datas.add(product);
}
return datas;
}
}
}