XRecyclerView+Retrofit+OKhttp请求分页加载

依赖

	implementation 'com.squareup.okhttp3:okhttp:3.1.2'
    implementation 'com.squareup.retrofit2:retrofit:2.0.2'
    implementation 'com.facebook.fresco:fresco:0.12.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.jcodecraeer:xrecyclerview:1.3.2'

定义一个接口

public interface Api {
    @GET("meinv/?key=71e58b5b2f930eaf1f937407acde08fe&num=10")
    Call getMovie(@Query("page") int page);
}

封装工具类

public class HttpUtil {
    public static HttpUtil getInstance(){
        return httpUtilInter.httpUtil;
    }
    private static class httpUtilInter{
        private static HttpUtil httpUtil=new HttpUtil();
    }

    public final Api api;

    private HttpUtil() {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://api.tianapi.com/")
                .build();
        api = retrofit.create(Api.class);
    }
}

请求

public class MainPresenter extends BasePresenter>{

    private final HttpUtil httpUtil;

    public MainPresenter() {
        httpUtil = HttpUtil.getInstance();
    }
    public void loadMovie(int page){
        Call movie = httpUtil.api.getMovie(page);
        movie.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Movie body = response.body();
                getView().onSuegss(body);
            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }
}

主页面

public class MainActivity extends AppCompatActivity implements IMainView {

    private XRecyclerView recyclerView;
    private MainPresenter mainPresenter;
    private int page=1;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    private void initView() {
        recyclerView = findViewById(R.id.list);
    }
    private void initData() {
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadingMoreEnabled(true);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(gridLayoutManager);
        mainPresenter = new MainPresenter();
        mainPresenter.setView(this);
        mainPresenter.loadMovie(page);
        myAdapter = new MyAdapter(this);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page=1;
                        mainPresenter.loadMovie(page);
                        recyclerView.refreshComplete();
                    }
                },2000);

            }

            @Override
            public void onLoadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        mainPresenter.loadMovie(page);
                        recyclerView.loadMoreComplete();
                    }
                },2000);
            }
        });




    }
    @Override
    public void onSuegss(final Movie movie) {
        final List result = movie.getNewslist();
        myAdapter.setData(result);
    }
    Handler handler=new Handler();

    @Override
    public void Err(String err) {

    }
}

效果图如下:
XRecyclerView+Retrofit+OKhttp请求分页加载_第1张图片

你可能感兴趣的:(XRecyclerView+Retrofit+OKhttp请求分页加载)