compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.zhy:okhttputils:2.0.0'
layout
<com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/smart" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </com.scwang.smartrefresh.layout.SmartRefreshLayout>
activity
private RecyclerView recyclerView; private List<String> list; private LinearLayoutManager linearLayoutManager; private MyAdapter adapter; private int page = 1; private SmartRefreshLayout refreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); } private void initData() { OkHttpUtils .get() .url(HttpUtil.URL_LIST) .addParams("userId", "213132") .addParams("page", page + "") .addParams("pageSize", "10") .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { } @Override public void onResponse(String response, int id) { try { JSONObjectobj = new JSONObject(response); JSONArray data = obj.getJSONArray("data"); if (data != null) { for (int i = 0; i < data.length(); i++) { JSONObject obj2 = data.getJSONObject(i); String buyTitle = obj2.optString("note"); list.add("标题:" + buyTitle); } } adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } }); } private void findView() { recyclerView = (RecyclerView) findViewById(R.id.recyclerView); refreshLayout = (SmartRefreshLayout) findViewById(R.id.smart); list = new ArrayList<>(); adapter = new MyAdapter(list, this); linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(adapter); initData(); refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) {//上拉加载 page++;//每次上拉page+1 initData();//初始化数据 refreshlayout.finishLoadmore();//完成上拉加载 refreshlayout.finishRefresh();//完成下拉刷新 } @Override public void onRefresh(RefreshLayout refreshlayout) {//下拉刷新 page = 1;//page恢复到1页 list.clear();//list清空一下 initData();//重新初始化数据 refreshlayout.finishRefresh();//完成刷新操作 } }); }