1.添加gradle
//下拉刷新
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.3'
2.mainActivity
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.example.zixun_.Apapter.VideoPter;
import com.example.zixun_.listener.OnItemChildClickListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity implements OnItemChildClickListener {
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private VideoPter videoAdapter;
private List<String> list=new ArrayList<>();
private RefreshLayout refreshLayout;
private Handler mhandler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
//放在这里面就在UI朱线程里面执行
switch (msg.what){
case 0:
videoAdapter.setDatas(list);
videoAdapter.notifyDataSetChanged();//通知recyview刷新页面
break;
case 1:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected int initLayout() {
return R.layout.activity_main;
}
@Override
protected void initView() {
recyclerView=(RecyclerView) findViewById(R.id.r1);
refreshLayout=(RefreshLayout)findViewById(R.id.refreshLayout);//上拉刷新
}
@Override
protected void initData() {
linearLayoutManager=new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//垂直排列
recyclerView.setLayoutManager(linearLayoutManager);
videoAdapter=new VideoPter(MainActivity.this);
videoAdapter.setOnItemChildClickListener(this);//视频添加点击事件
recyclerView.setAdapter(videoAdapter);
//下拉刷新第三方插件)
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
getList(true);
}
});
// 上拉加载
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
getList(false);
}
});
getList(true);
}
public void getList(boolean isRefresh){
if (isRefresh) {
list=new ArrayList<>();
for (int a=0;a<5;a++){
list.add("a"+a);
}
refreshLayout.finishRefresh(true);
} else {
for (int a=0;a<5;a++){
list.add("a"+a);
}
refreshLayout.finishLoadMore(true);
}
mhandler.sendEmptyMessage(0);//通知handel添加四局
}
@Override
public void onItemChildClick(int position) {
}
3.布局:activity_main
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/refreshLayout"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/r1"
>
</androidx.recyclerview.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
4.