xrecyclerview 下拉刷新上拉加载

依赖
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
implementation 'com.android.support:design:27.1.0' 解决编译有误

写入适配器

//刷新
public  void refresh(List temlist){
    this.list.clear();
    this.list.addAll(temlist);
}
//加载更多
public  void loadMore(List list){
    this.list.addAll(list);
    notifyDataSetChanged();
}

写在MainActivivy

package com.example.com.day_one;

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

import com.example.com.day_one.adapter.MyAdapter;
import com.example.com.day_one.bean.Databean;
import com.example.com.day_one.presenter.MyDataPresenter;
import com.example.com.day_one.view.DataView;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements DataView{
    private int num=10;
    private String url = "http://api.tianapi.com/social/?key=c6250c3cef097ff0a85f987ed712c5f4&num="+num;
    private MyDataPresenter myDataPresenter;
    private boolean isRefresh = true;
    private MyAdapter myAdapter;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                setadapter();
            }
        }
    };
    private List allList = new ArrayList<>();
    private XRecyclerView mRlv;
    private List tempList;

    private void setadapter() {
        if(isRefresh){
            myAdapter=new MyAdapter(this,tempList);
            mRlv.setAdapter(myAdapter);
            myAdapter.refresh(allList);
            mRlv.refreshComplete();//设置刷新完成
        }else{
            if(myAdapter!=null){
                myAdapter.loadMore(allList);
                mRlv.loadMoreComplete();//设置加载更多完成
            }
        }
        if (myAdapter==null){
            return;
        }

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fresco.initialize(this);
        initView();
        myDataPresenter=new MyDataPresenter(this);
        myDataPresenter.getData(url);
    }

    private void initView() {
        mRlv =  findViewById(R.id.rlv);
        //设置布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
        LinearLayoutManager layoutManager1=new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
        mRlv.setLayoutManager(layoutManager1);
        mRlv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //刷新
                isRefresh=true;
                num=10;
                myDataPresenter.getData(url);
            }

            @Override
            public void onLoadMore() {
                //加载更多
                isRefresh=false;
                num+=10;
                String url = "http://api.tianapi.com/social/?key=c6250c3cef097ff0a85f987ed712c5f4&num="+num;
                myDataPresenter.getData(url);
            }
        });
    }

    @Override
    public void showData(Databean databean) {
         tempList = databean.getNewslist();
        allList.addAll(tempList);
        Message message= Message.obtain();
        message.what=1;
        handler.sendMessage(message);
    }
    //创建适配器


}

你可能感兴趣的:(xrecyclerview 下拉刷新上拉加载)