mSwipeRefreshLayout=messageLayout.findViewById(R.id.swipe_fresh);
mSwipeRefreshLayout.setRefreshing(true);
private void initView(){
mSwipeRefreshLayout.setOnRefreshListener(() -> {
index();
});
}
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/swipe_fresh"
xmlns:android="http://schemas.android.com/apk/res/android"/>
下拉刷新就这么简单
上拉的话
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"/>
这事要刷新发起的请求
public void refresh (Integer page,Integer type ){
String url = "
MyHttp.okHttpGet(mContext,url,new MyCallBackUtil(){
public void onSuccess(Context context, JSONObject response) {
try {
Log.d(TAG, "获取收益信息" + response);
items = response.getJSONArray("items");
notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
mNestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (scrollY > oldScrollY) {
// 向下滑动
}
if (scrollY < oldScrollY) {
// 向上滑动
}
if (scrollY == 0) {
// 顶部
}
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
// 底部
Log.d(TAG, "触发: ");
new Handler().postDelayed(new Runnable(){
public void run() {
sendMoreRequest();
}
}, 1000);
}
});
mRecyclerView.setAdapter(mAdaptaer);
通过监听滚动判断是否到了底部
getMeasuredHeight()是实际View的大小,与屏幕无关
而getHeight的大小此时则是屏幕的大小
当超出屏幕后,getMeasuredHeight()等于getHeight()加上屏幕之外没有显示的大小
也就是滚动的距离等于总长度减去一个屏幕的大小 就是到了底部;
这么减应该是负的 2减1是1 1减2是负1 所以理解起来挺容易
写在定时里面是为了延迟触发