public class Main2Activity extends AppCompatActivity { private XRecyclerView recyclerView; private OkHttpUiltrt anInt; private List<Lei.DataBean> list=new ArrayList<>(); private int qq=1; private String url="http://www.xieast.com/api/news/news.php?page="+qq; private Handler mHandler; private MyAdapter myAdapter; private List<Lei.DataBean> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); recyclerView = findViewById(R.id.recycler); anInt = OkHttpUiltrt.getInt(); initOKHttp(); shangxialashuxin(); } private void shangxialashuxin() { recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { qq++; new Handler().postDelayed(new Runnable() { @Override public void run() { list.clear(); list.addAll(data); myAdapter.notifyDataSetChanged(); recyclerView.refreshComplete(); initOKHttp(); } },3000); } @Override public void onLoadMore() { new Handler().postDelayed(new Runnable() { @Override public void run() { list.clear(); myAdapter.notifyDataSetChanged(); recyclerView.refreshComplete(); initOKHttp(); } },2000); } }); } private void initOKHttp() { anInt.asyncJsonStringByURL(url, new OkHttpUiltrt.Func1() { @Override public void onResponse(String result) { Gson gson = new Gson(); Lei lei = gson.fromJson(result, Lei.class); data = lei.getData(); list.addAll(data); myAdapter = new MyAdapter(list, Main2Activity.this); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(Main2Activity.this, LinearLayoutManager.VERTICAL, false)); } }); } }
XRecyclerView使用 上下拉刷新
最新推荐文章于 2021-12-01 14:06:08 发布