MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView mLv;
private int put = 1;
private ArrayList<Fuli_bean.ResultsBean> list;
private Shipeiqi shipeiqi;
private SmartRefreshLayout mRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
@SuppressLint("CheckResult")
private void initData() {
Retrofit build = new Retrofit.Builder()
.baseUrl(FuliServer.Fuli)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
FuliServer fuliServer = build.create(FuliServer.class);
Observable<Fuli_bean> fuli = fuliServer.getFuli(put);
fuli.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Fuli_bean>() {
@Override
public void accept(Fuli_bean fuli_bean) throws Exception {
list.addAll(fuli_bean.getResults());
shipeiqi.setList(list);
shipeiqi.notifyDataSetChanged();
}
});
}
private void initView() {
mLv = (RecyclerView) findViewById(R.id.lv);
mRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
list = new ArrayList<>();
shipeiqi = new Shipeiqi(list, this);
mLv.setAdapter(shipeiqi);
mLv.setLayoutManager(new LinearLayoutManager(this));
mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
put ++ ;
initData();
shipeiqi.notifyDataSetChanged();
refreshLayout.finishLoadMore(true);//加载完成
}
});
//刷新
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
list.clear();
initData();
shipeiqi.notifyDataSetChanged();
refreshLayout.finishRefresh(true);//刷新完成
}
});
}
}
xml里写
<com.scwang.smartrefresh.layout.SmartRefreshLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refreshLayout">
<com.scwang.smartrefresh.header.DeliveryHeader android:layout_height="wrap_content" android:layout_width="match_parent"/>
<android.support.v7.widget.RecyclerView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/lv" android:overScrollMode="never"/>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter android:layout_height="wrap_content" android:layout_width="match_parent" app:srlClassicsSpinnerStyle="Translate"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
依赖
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'//没有使用特殊Header,可以不加这行