上拉刷新,下拉加载 连接:https://github.com/823546371/PullToRefresh

package com.android.hp.refreshdemo;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.hp.refreshdemo.adapter.MyAdapter;
import com.android.hp.refreshdemo.bean.ContentBean;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recy;
    private PullToRefreshLayout activity_main;
    private List data;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        myAdapter = new MyAdapter(this,data);
        recy.setAdapter(myAdapter);
        final String url = "http://v.juhe.cn/toutiao/index?type=top&key=097060266650f67b2cebd2a06aded587";
        initData(url);
        activity_main.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.clear();
                        myAdapter.notifyDataSetChanged();
                        initData(url);
                        // 结束刷新
                        activity_main.finishRefresh();
                    }
                }, 2000);
            }

            @Override
            public void loadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData(url);
                        // 结束加载更多
                        activity_main.finishLoadMore();
                    }
                }, 2000);
            }
        });

    }

    private void initView() {
        recy = (RecyclerView) findViewById(R.id.recy);
        activity_main = (PullToRefreshLayout) findViewById(R.id.activity_main);
        recy.setLayoutManager(new LinearLayoutManager(this));
        data = new ArrayList<>();
    }

    private void initData(String url) {

        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Gson gson = new Gson();
                final ContentBean contentBean = gson.fromJson(string, ContentBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        data.addAll(contentBean.getResult().getData());
                        myAdapter.notifyDataSetChanged();
                    }
                });

            }
        });

    }
   
}

你可能感兴趣的:(上拉刷新,下拉加载 连接:https://github.com/823546371/PullToRefresh)