Android的XrecyclerView的分页加载

此页面主要是考虑到下拉和加载用的是同一个网络请求,我们使用来了一个变量来分别一下是上拉还是加载

package com.bwie.test.yuekaomoni;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.test.yuekaomoni.utils.GsonObjectCallback;
import com.bwie.test.yuekaomoni.utils.OkHttp3Utils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

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

import okhttp3.Call;


public class Fragment1 extends Fragment{
    XRecyclerView recyclerView;
    int pager=21;
    private String path;
    List beas;
    HomeAdapter homeAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_1,null);
        recyclerView=view.findViewById(R.id.recycler);
        beas=new ArrayList<>();
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pager=21;
                getDate(pager,"");

            }

            @Override
            public void onLoadMore() {
                pager=pager+1;
                getDate(pager,"load");
            }
        });
        getDate(pager,"");
        return view;
    }
    private void getDate(int pager, final String type)
    {
        path = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+pager+"&size=10&offset=0";
        OkHttp3Utils.getInstance().doGet(path,new GsonObjectCallback()
        {

            @Override
            public void onUi(Beans beans) {
                List song_list = beans.getSong_list();

                if (type.equals("load")){
                    beas.addAll(song_list);
                    homeAdapter.notifyDataSetChanged();
                    recyclerView.loadMoreComplete();

                }else{
                    if (homeAdapter==null){
                        beas=song_list;
                        homeAdapter = new HomeAdapter(beas, getActivity());
                        recyclerView.setAdapter(homeAdapter);
                    }
                    homeAdapter.notifyDataSetChanged();
                    recyclerView.refreshComplete();
                }

            }

            @Override
            public void onFailed(Call call, IOException e) {

            }

        });

    }

}


你可能感兴趣的:(Android的XrecyclerView的分页加载)