RecyclerView下拉刷新,上拉加载

RecyclerView外部嵌套SmartRefreshLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/c_F8F9FC"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <include layout="@layout/layout"
        android:id="@+id/basetop"/>

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        app:layout_constraintTop_toBottomOf="@+id/basetop"
        android:id="@+id/srl_fresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViews"
            app:layout_constraintTop_toBottomOf="@+id/ll2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >


        </android.support.v7.widget.RecyclerView>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>

代码定义当前page,刷新=1,加载+1

public class TuisongActivity extends BaseActivity {
    @BindView(R.id.srl_fresh)
    SmartRefreshLayout srfresh;
    @BindView(R.id.recyclerViews)
    RecyclerView rvList;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    private int currentPage=1;
    private List<TuisongBack.DataBean> mDatas=new LinkedList<>();;
    private TuisongAdapter recycleAdapter;
    private String TAG="TuisongActivity";
    private Handler mHandler=new Handler();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tuisong);
        ButterKnife.bind(this);
        tv_title.setText("信息推送");
        aboutAapter();
        initData();
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
    private void aboutAapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(TuisongActivity.this);
        //设置布局管理器
        rvList.setLayoutManager(layoutManager);
        //设置为垂直布局,这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        rvList.setNestedScrollingEnabled(false);
        rvList.setHasFixedSize(true);
        rvList.setFocusable(false);
        recycleAdapter = new TuisongAdapter(this , mDatas );
        rvList.setAdapter(recycleAdapter);

        srfresh.setRefreshHeader(new MaterialHeader(this));
        srfresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                //延时展示
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage=1;
                        mDatas.clear();
                        initData();
                        refreshlayout.finishRefresh();
                    }
                },0);

            }
        });
        srfresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(final RefreshLayout refreshlayout) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage++;
                        initData();
                        refreshlayout.finishLoadMore();
                    }
                },500);
            }
        });
        // srfresh.setEnableLoadMore(true); 禁止加载更多
    }
 private void initData() {
     
        Request request = new Request.Builder()
               .url(NetUtils.gettuiinfoApi+"/"+currentPage)
              .build();
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new TokenInterceptor())//添加获取token的拦截器
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {//回调的方法执行在子线程。
                     final TuisongBack rdb= new Gson().fromJson(info, TuisongBack.class);
                    if(1000==(rdb.getCode())){
                        runOnUiThread(new Runnable()
                        {
                            public void run() {
                                mDatas.addAll(rdb.getData());
                                recycleAdapter.notifyDataSetChanged();
                            } });}}}}); }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值