首先进行的依赖导入
implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19’
implementation ‘com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-19’
第二步进行布局设置控件,在XML布局中(嵌套时候必须用线性布局进行嵌套子布局才可以用SmartRefreshLayout在嵌套到最外层)
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
第三步在view层进行找控件,开始调用上拉加载和下拉刷新的方法
public class Home extends Fragment implements HomeContract.HomeView {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment1, null, false);
//下拉刷新
smart.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
persenter.requestdata(trim, page, count);
madapter.notifyDataSetChanged();//刷新适配器
smart.finishRefresh(true);
Toast.makeText(getContext(), “刷新成功”, Toast.LENGTH_SHORT).show();
}
});//下拉刷新
//上拉加载
smart.setOnLoadMoreListener( new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
page++;
persenter.requestdata(trim, page, count);//请求数据
madapter.notifyDataSetChanged();//刷新适配器
smart.finishLoadMore( true );
Toast.makeText( getContext(), “加载完成”, Toast.LENGTH_SHORT ).show();
}
} );//上拉加载
}