官网:https://github.com/scwang90/SmartRefreshLayout
在 build.gradle 中添加依赖:
implementation 'androidx.appcompat:appcompat:1.0.0' //必须 1.0.0 以上
implementation 'io.github.scwang90:refresh-layout-kernel:2.0.5' //核心必须依赖
implementation 'io.github.scwang90:refresh-header-classics:2.0.5' //经典刷新头
implementation 'io.github.scwang90:refresh-header-radar:2.0.5' //雷达刷新头
implementation 'io.github.scwang90:refresh-header-falsify:2.0.5' //虚拟刷新头
implementation 'io.github.scwang90:refresh-header-material:2.0.5' //谷歌刷新头
implementation 'io.github.scwang90:refresh-header-two-level:2.0.5' //二级刷新头
implementation 'io.github.scwang90:refresh-footer-ball:2.0.5' //球脉冲加载
implementation 'io.github.scwang90:refresh-footer-classics:2.0.5' //经典加载
如果使用 AndroidX 先在 gradle.properties 中添加,两行都不能少噢~
android.useAndroidX=true
android.enableJetifier=true
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.scwang.smart.refresh.layout.SmartRefreshLayout
android:id="@+id/refreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.Text2Activity">
<ListView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:dividerHeight="20dp" />
</com.scwang.smart.refresh.layout.SmartRefreshLayout>
</LinearLayout>
MainActivity:
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SmartRefreshLayout mRefreshlayout;
private ListView mRecyclerView;
private Adapters adapters;
private int i = 10;
private List<String> mlist = new ArrayList<>();
//static 代码段可以防止内存泄露
static {
//设置全局的Header构建器
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
layout.setPrimaryColorsId(R.color.colorAccent2, R.color.colorAccent1);//全局设置主题颜色
return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header,默认是 贝塞尔雷达Header
}
});
//设置全局的Footer构建器
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
@SuppressLint("ResourceAsColor")
@Override
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//指定为经典Footer,默认是 BallPulseFooter
// return new ClassicsFooter(context).setDrawableSize(20);
return new BallPulseFooter(context).setAnimatingColor(R.color.colorAccent2);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
JZ(i);
mRefreshlayout = (SmartRefreshLayout) findViewById(R.id.refreshlayout);
mRecyclerView = (ListView) findViewById(R.id.recyclerView);
mRefreshlayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
i = 10;
JZ(i);
adapters.notifyDataSetChanged();
Log.i("TAG", "当前" + i);
// mRefreshlayout.finishRefresh(1000/*,false*/);//传入false表示刷新失败
mRefreshlayout.finishRefresh();
}
});
mRefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
i = i + 10;
JZ(i);
adapters.notifyDataSetChanged();
Log.i("TAG", "当前" + i);
// mRefreshlayout.finishLoadMore(1000/*,false*/);//传入false表示刷新失败
mRefreshlayout.finishLoadMore();
}
});
adapters = new Adapters();
mRecyclerView.setAdapter(adapters);
}
class Adapters extends BaseAdapter {
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Herdview herdview = null;
if (convertView == null) {
herdview = new Herdview();
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
convertView.setTag(herdview);
} else {
herdview = (Herdview) convertView.getTag();
}
herdview.textViews = convertView.findViewById(R.id.names);
herdview.textViews.setText(mlist.get(position));
return convertView;
}
}
class Herdview {
TextView textViews;
}
private void JZ(int a) {
mlist.clear();
for (int i = 0; i < a; i++) {
mlist.add("LXD" + i);
}
}
}
item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/names"
android:layout_width="match_parent"
android:layout_height="30dp"
android:paddingLeft="10dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="20dp" />
</LinearLayout>