直接新建这个类
package com.wdx.center.view;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
import androidx.swiperefreshlayout.widget.CircularProgressDrawable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.wdx.center.R;
import com.wdx.common.utils.DensityUtils;
/**
* @ Description:
* @ Author: wdx
* @ CreateDate: 2020/9/7 15:17
*/
public class MySwipeRefreshLayoutextends SwipeRefreshLayout {
/**
* 滑动到最下面时的上拉操作
*/
private int mTounchslop;
/**
* ListView的加载中footer
*/
private ViewmListViewFooter;
// private ListView mListView;
private RecyclerViewmRecycleView;
/**
* 按下时的y坐标
*/
private int mYdown;
private int mYlast;
private boolean isLoading =false;
private OnLoadMoreListenermOnLoadMoreListener;
private TextViewmTvLoadMore;
private int mVisibleItemCount;
private int mTotalItemCount;
ContextmContext;
public MySwipeRefreshLayout(Context context) {
this(context, null);
mContext=context;
}
public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
init();
}
private void init() {
mTounchslop = ViewConfiguration.get(mContext).getScaledTouchSlop();
mListViewFooter = LayoutInflater.from(mContext).inflate(R.layout.footer_item, null);
mTvLoadMore =mListViewFooter.findViewById(R.id.tv_loadmore);
mTvLoadMore.setText("加载更多");
LayoutParams layoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
mListViewFooter.setLayoutParams(layoutParams);
}
public void setRecycleView(RecyclerView mRecycleView){
this.mRecycleView = mRecycleView;
}
/**
* 获取ListView对象
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mYdown = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
mYlast = (int) ev.getY();
break;
case MotionEvent.ACTION_UP:
if (canLoad()) {
loadData();
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
private boolean canLoad() {
return !isLoading && isPullup()&&isLong();
}
private boolean isLong(){
// recyclerView.computeVerticalScrollExtent() //显示区域的高度
// recyclerView.computeVerticalScrollOffset() //已经向下滚动的距离,为0时表示已处于顶部
// recyclerView.computeVerticalScrollRange() //整体的高度,注意是整体,包括在显示区域之外的
return mRecycleView.computeVerticalScrollExtent() +mRecycleView.computeVerticalScrollOffset()
>=mRecycleView.computeVerticalScrollRange();
}
private boolean enableBottomLoad() {
return !isLoading && isBottom();
}
private boolean isBottom() {
//.getLastVisiblePosition()
if (mRecycleView !=null &&mRecycleView.getAdapter() !=null) {
return mVisibleItemCount
&& ((LinearLayoutManager)mRecycleView.getLayoutManager()).findLastVisibleItemPosition() ==mRecycleView.getAdapter().getItemCount() -1;
}
return false;
}
private boolean isPullup() {
return mYdown -mYlast >=mTounchslop;
}
public void setLoading(boolean loading) {
}
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
mOnLoadMoreListener = listener;
}
public interface OnLoadMoreListener {
void onLoadMore();
}
private void loadData() {
if (mOnLoadMoreListener !=null) {
setLoading(true);
mOnLoadMoreListener.onLoadMore();
RelativeLayout.LayoutParams layoutParams = getRelativeParms();
if(mListViewFooter.getParent()==null){
((RelativeLayout)this.getParent()).addView(mListViewFooter,layoutParams);
}else {
((RelativeLayout)this.getParent()).removeView(mListViewFooter);
}
}
}
public RelativeLayout.LayoutParamsgetRelativeParms(){
RelativeLayout.LayoutParams rLParams =
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
return rLParams;
}
}
------------------
记得setRecycleView 外部调用一下
然后在xml中嵌套这个布局 至于加载更多
使用类
XXXX implements SwipeRefreshLayout.OnRefreshListener
@Override
public void onRefresh() {
//viewModel.setdatalist();
}
swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
// viewModel.setOnload();
Toast.makeText(MainActivity.this, "loadmore", Toast.LENGTH_SHORT).show();
}
});
---------------
调用处xml布局
<com.wdx.center.view.MySwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/rcv_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.wdx.center.view.MySwipeRefreshLayout>
至于footerItem
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:id="@+id/tv_loadmore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="加载更多">
</androidx.constraintlayout.widget.ConstraintLayout>