?1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/srl_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="10dp"
app:srlAccentColor="#00000000"
app:srlEnablePreviewInEditMode="true"
app:srlPrimaryColor="#00000000"
tools:ignore="MissingClass">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</RelativeLayout>
2.代码
public class TestFragment extends LazyLoadFragment {
private ListAdapter listAdapter;
private SmartRefreshLayout srl_id;
private RecyclerView rv_id;
private List<String> list;
private int page = 1;
private int count = 10;
@Override
protected int setContentView() {
return R.layout.fragment_test;
}
@Override
protected void lazyLoad() {
srl_id = findViewById(R.id.srl_id);
rv_id = findViewById(R.id.rv_id);
rv_id.setLayoutManager(new LinearLayoutManager(getActivity()));
list = new ArrayList<>();
for (int i = 1; i <= count ; i++){
list.add(i+1+"");
}
listAdapter = new ListAdapter(getActivity());
rv_id.setAdapter(listAdapter);
listAdapter.setList(list);
//刷新
srl_id.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(2000);
list.clear();
for (int i = 1; i <= count ; i++){
list.add(i+1+"");
}
}
});
//加载
srl_id.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadmore(1000);
count =+ 10;
page++;
for (int i = 1; i <= count ; i++){
list.add(i+1+"");
}
listAdapter.notifyDataSetChanged();
}
});
}
}
3.adapter
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.Myvh> {
Context context;
List<String> list;
public ListAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<String> list) {
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item_list, null);
return new Myvh(inflate);
}
@Override
public void onBindViewHolder(@NonNull Myvh holder, int position) {
holder.item_tv.setText(position+1+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final TextView item_tv;
public Myvh(@NonNull View itemView) {
super(itemView);
item_tv = itemView.findViewById(R.id.item_tv);
}
}
}
|